I am running a test to make sure emails are not sent to certain users:
it "does not send reminders to users who are not subscribed" do
expect(ReminderMailer).not_to receive(:send_reminder).with("whatever", user.id).and_return(double(deliver_now: true))
book.send_reminders("whatever", user.id)
end
Book.rb
def send_reminders(medium, user_id)
ReminderMailer.send_reminder(medium, user_id).deliver_now
end
But the current test is saying:
and_return is not supported with negative message expectations
If I remove the and_return portion:
it "does not send reminders to users who are not subscribed" do
expect(ReminderMailer).not_to receive(:send_reminder).with("whatever", user.id)
book.send_reminders("whatever")
end
Then the test fails because:
undefined method `deliver_now' for nil:NilClass
How can I test that an action mailer method with specific parameters are not called?
There are some problems with your code:
The number of arguments passed to method send_reminders
seems wrong (you pass only 1 argument 'whatever' but no user id)
I don't see any condition to filter user, is it in ReminderMailer
's method send_reminder
? So it will call send_reminder
anyway, just won't send mail, right? It's not what you expected.
If so, you could try this:
expect {
book.send_reminders("whatever")
}.not_to change(ActionMailer::Base.deliveries, :count)
It will expect no mail was actually sent. Good luck bro!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With