I''m try to stub the following in my view code but keep running into problems with unexpected messages. I''ve tried stubbing and mocking in many combinations but can''t find away past this. Here is the code below I am trying to spec ... - if page.parent_id.nil? %td - else %td= Page.criteria.id(page.parent_id).first.title ... I receive this error Mock "MongoidCriteria_1004" received unexpected message :title with (no args) when I do the following Page.stub_chain(:criteria, :id).and_return(mock_model("MongoidCriteria", :first => mock_model("Page", :title => "foobar"))) I''ve tried every combination I can think of but can''t figure out how to stub this.
On Thu, Mar 24, 2011 at 10:22 AM, amkirwan <amkirwan at gmail.com> wrote:> I''m try to stub the following in my view code but keep running into > problems with unexpected messages. I''ve tried stubbing and mocking in > many combinations but can''t find away past this. Here is the code > below I am trying to spec > > ... > - if page.parent_id.nil? > %td > - else > %td= Page.criteria.id(page.parent_id).first.title > ... > > I receive this error Mock "MongoidCriteria_1004" received unexpected > message :title with (no args) when I do the following > > Page.stub_chain(:criteria, :id).and_return(mock_model("MongoidCriteria", > :first > => mock_model("Page", :title => "foobar"))) > > I''ve tried every combination I can think of but can''t figure out how > to stub this. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersTry this: Page.stub_chain(''criteria.id.first.title'').and_return(''the title'') -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110324/0fca77fe/attachment.html>
no unfortunately that does not work either. I receive the error: undefined method `title'' for #<Object:0x00000101cfd080> -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110325/86ee2cea/attachment.html>
Michael B. Klein
2011-Mar-26 06:11 UTC
[rspec-users] stubbing mongoid::criteria in view code
What happens if you Page.stub_chain(:criteria, :id).and_return([mock_model("Page", :title => "foobar")]) (i.e., replace the mocked MongoidCriteria with a single-element Array containing the mocked Page) On Fri, Mar 25, 2011 at 8:18 AM, amkirwan <amkirwan at gmail.com> wrote:> no unfortunately that does not work either. I receive the error: undefined > method `title'' for #<Object:0x00000101cfd080> > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110325/8bacde6c/attachment.html>
that did it thanks. On Mar 26, 2:11?am, "Michael B. Klein" <mbkl... at gmail.com> wrote:> What happens if you > > Page.stub_chain(:criteria, :id).and_return([mock_model("Page", :title => > "foobar")]) > > (i.e., replace the mocked MongoidCriteria with a single-element Array > containing the mocked Page) > > > > On Fri, Mar 25, 2011 at 8:18 AM, amkirwan <amkir... at gmail.com> wrote: > > no unfortunately that does not work either. I receive the error: undefined > > method `title'' for #<Object:0x00000101cfd080> > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users