user938363
user938363

Reputation: 10350

Wrong with mock_model in rspec?

Here is the rspec code for testing show in customers controller:

it "'show' should be successful" do
  #category = Factory(:category)
  #sales = Factory(:user)
  #customer = Factory(:customer, :category1_id => category.id, :sales_id => sales.id)
  category = mock_model('Category')
  sales = mock_model('User')
  customer = mock_model(Category, :sales_id => sales.id, :category1_id => category.id)

  get 'show' , :id => customer.id
  response.should be_success
end

Here is the error in rspec:

CustomersController GET customer page 'show' should be successful
     Failure/Error: get 'show' , :id => customer.id
     ActiveRecord::RecordNotFound:
       Couldn't find Customer with id=1003
     # c:in `find'
     # ./app/controllers/customers_controller.rb:59:in `show'
     # ./spec/controllers/customers_controller_spec.rb:50:in `block (3 levels) in <top (required)>'

The rspec test passes with the real record created by Factory (see #ed in rspec code)

What's wrong with the mock? Thanks.

Upvotes: 1

Views: 745

Answers (1)

apneadiving
apneadiving

Reputation: 115511

The spec is failing inside the controller's action which doesn't know anything about your mocks unless you told it explicitly.

Add this to your spec, before the get statement.

Customer.should_receive(:find).and_return(customer)

Upvotes: 2

Related Questions