8

I want to discover with BDD missing :include params for ActiveRecord::Base.find method. So my idea is to have in spec something like this:

ActiveRecord::Base.should_receive(:find).once.and_proxy_to_original_method
parent = SomeClass.find 34
parent.child.should be_loaded
parent.other_children.should be_loaded

If #child or #other_children associations are not eager loaded, expectation should fail with something like: "Expected ActiveRecord::Base.find to be invoked once but it was invoked 2 more times with following args: 1. ...; 2. ..."

Does anyone know if there's some matcher that works like this or how to make this.

Thanks

BurmajaM
  • 724
  • 5
  • 10
  • I have same issue. Really wish there was one. Useful for testing if valid? was invoked on a dependent model or something to ensure the hierarchy is validated/saved. – Dmitriy Likhten Jul 21 '11 at 04:05
  • possible duplicate of [Is there a less intrusive alternative to Rspec's \`should\_receive\`?](http://stackoverflow.com/questions/12159536/is-there-a-less-intrusive-alternative-to-rspecs-should-receive) – lulalala Mar 17 '14 at 07:53

1 Answers1

5

I think I had the same problem here. In your particular case I would do this which I find quite clean.

original_method = ActiveRecord::Base.method(:find)
ActiveRecord::Base.should_receive(:find).once do (*args)
  original_method.call(*args)
end

I believe you could extend the Rspec Mocks::MessageExpectation class to include the and_proxy_to_original_method method, shouldn't be too hard, but I haven't looked.

Community
  • 1
  • 1
joao
  • 3,517
  • 1
  • 31
  • 43