My question is about how to spec a callback for sending an email with the right way.
I have a model called Question
. After a question
is created, I have a callback after_create
to send an email.
I would like to create a rpsec to test this method. However, I already have specs for each Mailer, that verifies if the email is actually sent and the recipients.
So, I would like to know if inside the after_create
should I test the call of the send_mail
method or check if the last email sent is present? Because, like I said, the test for email itself is in the spec for the mailer spec/mailers/question_mailer_spec.rb
Model:
class Question < ActiveRecord::Base
after_create :send_email
def send_email
QuestionMailer.send_email(:questioned, self.id)
end
Rspec:
describe "#send_email" do
it "should sends email after create" do
expect(QuestionMailer).to receive(:send_email).with(:questioned, **question.id**)
question = create(:question)
end
end
Thanks!