Stubbing mongoid::criteria in view code

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?
- else

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”,
=> 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 [email protected] wrote:

rspec-users mailing list
[email protected]

Try this:

Page.stub_chain(‘’).and_return(‘the title’)

no unfortunately that does not work either. I receive the error:
method `title’ for #Object:0x00000101cfd080

What happens if you

Page.stub_chain(:criteria, :id).and_return([mock_model(“Page”, :title =>

(i.e., replace the mocked MongoidCriteria with a single-element Array
containing the mocked Page)

that did it thanks.