Search code examples
rspecexpectations

How do I deal with rspec should_receive replacing the method when I want the method to actually be executed?


I'm new to using rspec should_receive and having a lot of problems because it "replaces" the method. For example:

UserMailer.should_receive(:new_order).with(order) 

gives undefined method `deliver' for nil:NilClass, since rspec makes the method stop working. How do I deal with this?


Solution

  • You deal with this by adding .and_call_original to your method chain as follows:

    UserMailer.should_receive(:new_order).with(order).and_call_original
    

    as documented in https://www.relishapp.com/rspec/rspec-mocks/v/2-13/docs/message-expectations/calling-the-original-method and discussed in Is there a less intrusive alternative to Rspec's `should_receive`?