Rails Tutorial ch. 10 Test 10.20 failure

风格不统一 提交于 2019-12-11 03:18:31

问题


I have problem with test 10.20 in Rails Tutorial. After using command:

bundle exec rake test:mailers

I've received error:

Run options: --seed 6131

# Running:

F

Finished in 0.559132s, 1.7885 runs/s, 5.3655 assertions/s.

  1) Failure:
UserMailerTest#test_account_activation [/home/adam/workspace/sample_app    /test/mailers/user_mailer_test.rb:11]:
Expected: ["noreply@example.com"]
Actual: nil

1 runs, 3 assertions, 1 failures, 0 errors, 0 skips

I've thoroughly checked my code and can't find the impropriety but I'm obviously doing it wrong.

Here's my user_mailer:

class UserMailer < ActionMailer::Base

  def account_activation(user)
    @user = user
    mail to: user.email, subject: "Account activation"
  end

  def password_reset
    @greeting = "Hi"

    mail to: "to@example.org"
  end
end

user_mailer_test:

require 'test_helper'

class UserMailerTest < ActionMailer::TestCase

  test "account_activation" do
    user = users(:michael)
    user.activation_token = User.new_token
    mail = UserMailer.account_activation(user)
    assert_equal "Account activation", mail.subject
    assert_equal [user.email], mail.to
    assert_equal ["noreply@example.com"], mail.from
    assert_match user.name,               mail.body.encoded
    assert_match user.activation_token,   mail.body.encoded
    assert_match CGI::escape(user.email), mail.body.encoded
  end
end

I've added this line to config/environments/test.rb:

config.action_mailer.default_url_options = { host: 'example.com' }

Can I have some advise?


回答1:


You need to add :from to this line

mail to: user.email, subject: "Account activation"

like this

mail to: user.email, from: "noreply@example.com", subject: "Account activation"



回答2:


All references to the User mailer should start with

class UserMailer < ApplicationMailer

This is due to recent changes in Rails 4.2. I missed a couple of references when I updated the tutorial last week, but these have now been corrected. Be sure you also include the Application mailer as shown in Listing 10.8. In fact, it's probably a good idea to remove the mailers and regenerate them from scratch to make sure your code conforms to the structure expected by Rails 4.2.




回答3:


Simply edit the user_mailer.rb file from this:

class UserMailer < ActionMailer::Base

to this:

class UserMailer < ApplicationMailer

The reason being is that the default from: address is in the application_mailer.rb file and it pulls from ActionMailer::Base



来源:https://stackoverflow.com/questions/28614943/rails-tutorial-ch-10-test-10-20-failure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!