Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expect method call and proxy to original method with RSpec

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

like image 211
BurmajaM Avatar asked Mar 16 '11 02:03

BurmajaM


1 Answers

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.

like image 53
Joao Tavora Avatar answered Sep 28 '22 18:09

Joao Tavora