According to the Rails testing guide:
The goals of testing your mailer classes are to ensure that:
- emails are being processed (created and sent)
- the email content is correct (subject, sender, body, etc)
- the right emails are being sent at the right times
12.1.1 From All Sides
There are two aspects of testing your mailer, the unit tests and the functional tests. In the unit tests, you run the mailer in isolation with tightly controlled inputs and compare the output to a known value (a fixture.) In the functional tests you don't so much test the minute details produced by the mailer; instead, we test that our controllers and models are using the mailer in the right way. You test to prove that the right email was sent at the right time.
There likewise could be two aspects of testing Heya campaigns: unit and functional.
Unit Testing
I think I have a good approach for unit testing emails, using a similar API to Rails (this is currently working):
RSpec.describe SignupFirstRunCampaign, type: :campaign do
describe "welcome" do
it do
user = FactoryBot.create(:user)
email = SignupFirstRunCampaign.welcome(user)
assert_emails 1 do
email.deliver_now
end
assert_equal ["[email protected]"], email.from
assert_equal [user.email], email.to
assert_equal "Welcome to Honeybadger! ๐", email.subject
assert_ican email
end
end
end
Functional Testing
Unlike ActionMailer, Hey campaign emails are sent from Heya's scheduler rather than arbitrary places like Rails controllers. It would be nice to be able to test that the correct emails are sent at the correct times, similar to how we do in the internal scheduler tests. Something along these lines (this is pseudo-code):
test "it sends campaign emails on time" do
user = users(:first)
OnboardingCampaign.add(user)
assert_step OnboardingCampaign, :step_one, user
refute_step OnboardingCampaign, :step_two, user
TimeCop.travel(1.day.from_now)
assert_step OnboardingCampaign, :step_two, user
end
I'm not actually sure if this falls under functional testing or unit testing, since it's not really testing the integration between Heya and application components. In any case, I'm looking for feedback on it--would something like this be a good addition?