Reputation: 2663
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?
Upvotes: 0
Views: 491
Reputation: 29389
You can use allow
as described in https://relishapp.com/rspec/rspec-mocks/v/3-1/docs under 'Method Stubs':
before do
mail = double('email', deliver: true)
allow(MessageMailer).to receive(:message_notification) { mail }
end
You can also use receive_message_chain
as described in https://relishapp.com/rspec/rspec-mocks/docs/working-with-legacy-code/message-chains:
before { allow(MessageMailer).to receive_message_chain(:message_notification, :deliver => true) }
but note the associated warning about Law of Demeter, brittleness, etc.
Upvotes: 1