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]) Product.should_receive(:find_all_meeting_some_criteria_for_category).with(product.category_id) end -- Posted via http://www.ruby-forum.com/.
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 >
Looks like this line Product.stub(!find_all_by_category_id).with(anything()).and_return([product]) might be the culprit. It should be stub!(find... instead of stub(!find.... Regards, Craig -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080811/8dce641b/attachment.html>
And i think that stubbed classes or objects can not access the database. On Mon, Aug 11, 2008 at 3:31 PM, Craig Demyanovich <cdemyanovich at gmail.com> wrote:> Looks like this line > > Product.stub(!find_all_by_category_id).with(anything()).and_return([product]) > > might be the culprit. It should be stub!(find... instead of stub(!find.... > > Regards, > Craig-- Maur?cio Linhares http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) Jo?o Pessoa, PB, +55 83 8867-7208
On Mon, Aug 11, 2008 at 1:31 PM, Craig Demyanovich <cdemyanovich at gmail.com> wrote:> Looks like this line > > Product.stub(!find_all_by_category_id).with(anything()).and_return([product]) > > might be the culprit. It should be stub!(find... instead of stub(!find....Actually, stub!(:find....> > Regards, > Craig > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
David Chelimsky wrote:> On Mon, Aug 11, 2008 at 1:31 PM, Craig Demyanovich > <cdemyanovich at gmail.com> wrote: >> Looks like this line >> >> Product.stub(!find_all_by_category_id).with(anything()).and_return([product]) >> >> might be the culprit. It should be stub!(find... instead of stub(!find.... > > Actually, stub!(:find....Sorry that was a typo Its actually stub! in my code -- Posted via http://www.ruby-forum.com/.
On Mon, Aug 11, 2008 at 1:47 PM, John Mark <lists at ruby-forum.com> wrote:> David Chelimsky wrote: >> On Mon, Aug 11, 2008 at 1:31 PM, Craig Demyanovich >> <cdemyanovich at gmail.com> wrote: >>> Looks like this line >>> >>> Product.stub(!find_all_by_category_id).with(anything()).and_return([product]) >>> >>> might be the culprit. It should be stub!(find... instead of stub(!find.... >> >> Actually, stub!(:find.... > > Sorry that was a typo > > Its actually stub! in my codeThe 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?
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) 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) Product.find_all_meeting_some_criteria_for_category(product.category_id) end 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? Thanks Mark -- Posted via http://www.ruby-forum.com/.
On Mon, Aug 11, 2008 at 2:49 PM, John Mark <lists at ruby-forum.com> wrote:> 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.Can''t really help you if you''re submitting code that is different from the code that is causing you trouble. Please be more careful about this.> 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) > 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) > Product.find_all_meeting_some_criteria_for_category(product.category_id) > endOK - so here, the 2nd to last line sets an expectation that Product should receive : find_all_meeting_some_criteria_for_category with product.category_id, and then the last line makes that exact call. This means that none of your actual code is being executed here. Is that really what you have?> 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? > > > Thanks > Mark > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
David Chelimsky wrote:> On Mon, Aug 11, 2008 at 2:49 PM, John Mark <lists at ruby-forum.com> wrote: >>> 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. > > Can''t really help you if you''re submitting code that is different from > the code that is causing you trouble. Please be more careful about > this. > >> 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) >> 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) >> Product.find_all_meeting_some_criteria_for_category(product.category_id) >> end > > OK - so here, the 2nd to last line sets an expectation that Product > should receive : find_all_meeting_some_criteria_for_category with > product.category_id, and then the last line makes that exact call. > This means that none of your actual code is being executed here. Is > that really what you have?Thanks David for the effort you are making to address my questions. Next time I will definitely run the examples I am wrting here before I post them Yes the actual code gets called and thats where the nil error is getting generated from. The code works ok for a simple application with only the Product and Category model. The real code has a lot of associations and other complex logic which I did not want to post here as they will hide the problem I am trying to solve. I will investigate it further since now I know the problem could be caused by something else other than rspec. It just looks as if (based on logs) that no database call is made in the real application when I make this call with rspec (although it works ok without rspec) products = find_all_by_category_id(category_id) The error is generated in the statement following the above statement products.each do |product| saying products is nil -- Posted via http://www.ruby-forum.com/.
> Sorry again I forgot to add the last statementCould you just copy and paste your code? That would be infinitely more helpful. Pat
> It just looks as if (based on logs) that no database call is made in the > real application when I make this call with rspec (although it works ok > without rspec) > products = find_all_by_category_id(category_id) > The error is generated in the statement following the above statement > products.each do |product| > saying products is nilI should not have expected any database call as this is a stub. I will investigate further as there is something else causing the error other than rspec. -- Posted via http://www.ruby-forum.com/.