I was writing a method in Rails 6.x that would send me notifications when users signed up or did important actions, such as make a purchase.

In writing the tests for this I wanted to make sure an email was being sent when the action of interest happened on my models. Thanks to some lovely ActionMailer test helpers this was really easy. The catch was that you have to include ActionMailer::TestHelper in the test case to be able to use assert_emails.

class UserTest < ActiveSupport::TestCase

  class UserPurchases < UserTest
    include ActionMailer::TestHelper

    test "When a user makes a purchase a notification email is sent" do
      u = User.create
      p = Product.create(sku: "sku", days: 7)
      assert_emails 1 do
        assert u.purchase(p)
      end
    end
  end

end