Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An strange behaviour of should_receive... or mongoid... or me #222

Closed
ghost opened this issue Feb 6, 2013 · 4 comments
Closed

An strange behaviour of should_receive... or mongoid... or me #222

ghost opened this issue Feb 6, 2013 · 4 comments

Comments

@ghost
Copy link

ghost commented Feb 6, 2013

I have found an strange behaviour of should_receive which is this:

require 'spec_helper'

class Parent
  include Mongoid::Document
  embeds_one :child
end

class Child
  include Mongoid::Document
  embedded_in :parent

  def part
    true
  end

  def all
    part
  end
end

describe Child do
  let(:child) do
    Parent.new.build_child
  end
  describe '#all' do
    it 'calls part' do
      puts child
      child.should_receive(:part)
      child.all
    end
  end
end

It brings me this:

Failures:

  1) Child#all calls part
     Failure/Error: child.should_receive(:part)
       (#<Child:0x007feb7791c818>).part(any args)
           expected: 1 time
           received: 0 times

but when I change "Parent.new.build_child" to "Child.new" it passes! and both methods return instances of Child!. I don't know if i'm missing something or if it is a bug of rspec or perhaps mongoid but Im getting mad with this. (Fabrication also fails with Fabricate.build(:parent).child)

@alindeman
Copy link
Contributor

I think Mongoid, like many ORMs, returns some sort of proxy object that delegates some--but not all--calls through. This doesn't always play nicely with a mocking library like rspec-mocks.

I'd be game to investigate further if you pushed up a complete app that I can pull down and get running quickly.

@ghost
Copy link
Author

ghost commented Feb 6, 2013

@durran
Copy link

durran commented Feb 6, 2013

@alindeman I made a comment on the issue from a Mongoid side (https://github.com/mongoid/mongoid/issues/2788) - I don't believe there is anything that can be done from either framework to allow this expectation to work that is easily accomplished and makes sense to implement. I'd say you can go ahead and close.

@alindeman
Copy link
Contributor

@alindeman I made a comment on the issue from a Mongoid side (mongoid/mongoid#2788) - I don't believe there is anything that can be done from either framework to allow this expectation to work that is easily accomplished and makes sense to implement. I'd say you can go ahead and close.

Thanks for the response. I agree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants