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

前端 未结 2 1081
野趣味
野趣味 2021-01-28 01:15

I have something like:

value = nil

if some_condition
  value =my_object.do_stuff()
end

And in my test, I have the follwing:

My         


        
相关标签:
2条回答
  • 2021-01-28 01:23

    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.

    0 讨论(0)
  • 2021-01-28 01:37

    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

    0 讨论(0)
提交回复
热议问题