简体   繁体   中英

Rails Guides example for testing mailers?

The rails guides example code for testing mailers is as follows:

require 'test_helper'

class UserMailerTest < ActionMailer::TestCase
  tests UserMailer
  test "invite" do
    # Send the email, then test that it got queued
    email = UserMailer.create_invite('me@example.com',
                                     'friend@example.com', Time.now).deliver
    assert !ActionMailer::Base.deliveries.empty?

    # Test the body of the sent email contains what we expect it to
    assert_equal ['me@example.com'], email.from
    assert_equal ['friend@example.com'], email.to
    assert_equal 'You have been invited by me@example.com', email.subject
    assert_equal read_fixture('invite').join, email.body.to_s
  end
end

What is the purpose of this line and do I have to add it in my tests? --> tests UserMailer

Please check the tests method implementation

Here is what carlosantoniodasilva says about tests

You can use tests in mailers/controllers/helpers tests to define which mailer/controller/helper you're testing, in case it cannot be automatically guessed from the test name. eg, UserMailerTest automatically guesses UserMailer as the test subject, but you can explicitly say that or say it's a different name with the tests method. So having the line there is not entirely wrong, it's just being explicit about what Rails does automatically.

You can read the entire discussion on this very specific topic at Github

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM