[rspec-users] find_all_by returning nil

John Mark lists at ruby-forum.com
Mon Aug 11 15:49:19 EDT 2008

David Chelimsky wrote:
> On Mon, Aug 11, 2008 at 1:47 PM, John Mark <lists at ruby-forum.com> wrote:
>> Sorry that was a typo
>> Its actually stub! in my code
> The original example doesn't seem to do anything:
> it "should find products given a category" do
>   product = mock_model(Product, :id => 1, :category_id => 1)
>   Product.stub(!find_all_by_category_id).with(anything()).and_return([product])
>   Product.should_receive(:find_all_meeting_some_criteria_for_category).with(product.category_id)
> end
> It just sets up some objects but never actually calls an action. What
> is the error message you're getting?

Sorry again I forgot to add the last statement. Here is how the code 
should have looked like
 it "should find products given a category" do
   product = mock_model(Product, :id => 1, :category_id => 1)

I have created a new application with only the above 2 models and the 
rspec is working ok. It appears there are some extra information in my 
real application that is making it not to work properly. I have to 
investigate further on this.

The error I am getting is.
"You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error ocurred while evaluating nil.each"

aand the error line is line 2 below in the Product model
1. products = find_all_by_category_id(category_id)
2. products.each do |product|

Perhaps what I need answered for now is what could cause rspec return 
nil for an activerecord method that is supposed to return an array?


Posted via http://www.ruby-forum.com/.

More information about the rspec-users mailing list