[rspec-users] find_all_by returning nil
David Chelimsky
dchelimsky at gmail.com
Mon Aug 11 14:30:28 EDT 2008
On Mon, Aug 11, 2008 at 1:27 PM, John Mark <lists at ruby-forum.com> wrote:
> 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 value for 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])
Try this:
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
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
More information about the rspec-users
mailing list