Can I set an expectancy on should_receive and have it execute the original code?

ⅰ亾dé卋堺 提交于 2019-12-20 07:22:26

问题


I have something like:

value = nil

if some_condition
  value =my_object.do_stuff()
end

And in my test, I have the follwing:

MyObject.any_instance.should_receive(:do_stuff)

However, I'd like to just test that the method was called, and have it execute the original code. I'd like to NOT have to do it like:

MyObject.any_instance.should_receive(:do_stuff).and_return(:some_dummy_value)

Is there a way of doing that?


回答1:


There is and_call_original method:

MyObject.any_instance.should_receive(:do_stuff).and_call_original

See https://github.com/rspec/rspec-mocks#delegating-to-the-original-implementation




回答2:


I believe, that it's better to create object by FactoryGirl and than to test it. You can read how to make factories and so on. Example:

FactoryGirl.define do
  factory :my_object do
   [*here is your attributes*]
  end
end

So, after you created a factory, you should to create test where this method used and write this:

my_object = FactoryGirl.create(:my_object)
my_object.should_receive(:do_stuff)

Inside your code you will do that "do_stuff" with your "my_object" when u will run test.



来源:https://stackoverflow.com/questions/13602800/can-i-set-an-expectancy-on-should-receive-and-have-it-execute-the-original-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!