I want to test a method which includes the method MessageMailer.message_notification(self).deliver
. MessageMailer is a bit slow, and the test I am writing isn't testing it, so I'd like for deliver to not run in a test block.
Currently, I'm accomplishing this by stubbing it out.
before do
mail = double('email', deliver: true)
MessageMailer.stub(:message_notification) { mail }
end
But I've heard that stub is being replaced by double in the future, and MessageMailer.double(:message_notification)
fails with NoMethodError for double in my version of Rspec (2.12.2) . How should I rewrite this code with double to keep the deliver method from being called?
You can use
allow
as described in https://relishapp.com/rspec/rspec-mocks/v/3-1/docs under 'Method Stubs':You can also use
receive_message_chain
as described in https://relishapp.com/rspec/rspec-mocks/docs/working-with-legacy-code/message-chains:but note the associated warning about Law of Demeter, brittleness, etc.