# Rspec returns nil find\_all\_by

**URL:** <https://www.ruby-forum.com/t/rspec-returns-nil-find-all-by/196775>\
**Category:** Rails\
**Created:** [August 8, 2008, 10:27pm UTC](https://www.ruby-forum.com/t/rspec-returns-nil-find-all-by/196775 "2008-08-08T22:27:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [August 8, 2008, 10:27pm UTC](https://www.ruby-forum.com/t/rspec-returns-nil-find-all-by/196775/1 "2008-08-08T22:27:28Z")

</div>

The statement  
products = find\_all\_by\_category\_id(category\_id)  
in the Product model is returning nil with rspec.  
I am expecting it to to return the stub that I provided it with. It does  
not also work with fixtures.

Product model

def self.find\_all\_meeting\_some\_criteria\_for\_category(category\_id)  
products = find\_all\_by\_category\_id(category\_id)  
products.each do |product|  
…  
end  
end

Product spec

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
