You're getting mixed up between stubbing the methods on the class (Brand) and the instance (Brand.new)

Try the corrections below:

On 16 Oct 2009, at 05:25, Elza wrote:


I am starting with Rspec and I am having some errors in the Get/index
Can someone help me with this?

describe BrandsController, 'GET /index' do
 ##############################################################
 should_require_login :get, :index
 ##############################################################
 describe "authenticated user" do
   ##############################################################
   before do
     login_as_user
     @brands = mock_model(Brand)

@brand = mock_model(Brand)
@brands = [...@brand]

     @brands.stub!(:paginate).and_return(@brands)

Brand.stub!(:paginate).and_return(@brands)

   end
   ##############################################################
   def do_get
     get :index
   end

   ##############################################################
   it 'should load the brands' do
     @brands.should_receive(:paginate).with(

Brand.should_receive(:paginate).with(

       :conditions => {:active => 'true'},
       :order =>'brands.name'
     ).and_return(@brands)

     do_get

     assigns[:brands].should == @brands
   end
 end
end
************************************
My controller is
 def index
   # Build the SQL conditions object based on the parameters received.
   conditions = Sql::Conditions.new
   conditions.and('name LIKE ?', "%#{get_param(:name)}%")  if
get_param(:name)
   conditions.and('active = ?', true)
if !get_param(:include_inactive, :boolean)

   @brands =
       Brand.paginate(
         :conditions => conditions.to_sql,
         :order      => build_sort_conditions(:name, true),
         :page       => get_param(:page, :integer),
         :per_page   => 10
       )
 end
************************************************

The error is

'BrandsController GET /index authenticated user should load the brands'
FAILED
expected: #<Brand:0xfd2e56 @name="Brand_1058">,
    got: [] (using ==)
--
View this message in context: 
http://www.nabble.com/Help-me-with-Controllers-spec-tp25919605p25919605.html
Sent from the rspec-users mailing list archive at Nabble.com.

By the way, a more general comment if you don't mind me offering it, is that building SQL like this in a controller is considered a bit of an anti-pattern. You would be better off (and, funnily enough, find the mocks a lot easier to write) if you dreamed up a new method on Brand, something like Brand.paginage_for_name and pushed the logic that builds the SQL conditions down into the model.

cheers,
Matt

+447974 430184
m...@mattwynne.net
http://mattwynne.net

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to