Rails - How do you test ActionMailer sent a specific email in tests

后端 未结 7 2024
面向向阳花
面向向阳花 2020-12-23 12:58

Currently in my tests I do something like this to test if an email is queued to be sent

assert_difference(\'ActionMailer::Base.deliveries.size\', 1) do              


        
7条回答
  •  执念已碎
    2020-12-23 13:29

    When using the ActionMailer during tests, all mails are put in a big array called deliveries. What you basically are doing (and is sufficient mostly) is checking if emails are present in the array. But if you want to specifically check for a certain email, you have to know what is actually stored in the array. Luckily the emails themselves are stored, thus you are able to iterate through the array and check each email.

    See ActionMailer::Base to see what configuration methods are available, which you can use to determine what emails are present in the array. Some of the most suitable methods for your case probably are

    • recipients: Takes one or more email addresses. These addresses are where your email will be delivered to. Sets the To: header.
    • subject: The subject of your email. Sets the Subject: header.

提交回复
热议问题