UserMailerTest预期" noreply@example.com"实际:from@example.com

时间:2015-04-20 10:25:08

标签: ruby-on-rails

我该如何解决这个问题?我是Rails的新手

2)失败: UserMailerTest#test_password_reset [/Users/user/Documents/RoR/workspace/sample_app/test/mailers/user_mailer_test.rb:23]: 预期:[" noreply@example.com"]   实际:[" from@example.com"]

class UserMailer < ApplicationMailer

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

  def password_reset(user)
    @user = user
    mail to: user.email, subject: "Password reset"
  end
end

&GT;

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

  test "password_reset" do
    user = users(:michael)
    user.reset_token = User.new_token
    mail = UserMailer.password_reset(user)
    assert_equal "Password reset", mail.subject
    assert_equal [user.email], mail.to
    assert_equal ["noreply@example.com"], mail.from          #this is line 23
    assert_match user.reset_token,        mail.body.encoded
    assert_match CGI::escape(user.email), mail.body.encoded
  end
end

我更改了&#34; noreply@example.com"到&#34; from@example.com"。 现在,它会产生另一个错误。

  1) Failure:
UserMailerTest#test_account_activation [/Users/lldm0033/Documents/RoR/workspace/sample_app/test/mailers/user_mailer_test.rb:12]:
Expected /Michael\ Example/ to match "\r\n----==_mimepart_5534d70b387f1_b853fc04845e6cc9478e\r\nContent-Type: text/plain;\r\n charset=UTF-8\r\nContent-Transfer-Encoding: 7bit\r\n\r\nUserMailer#account_activation\r\n\r\n, find me in app/views/user_mailer/account_activation.text.erb\r\n\r\n\r\n----==_mimepart_5534d70b387f1_b853fc04845e6cc9478e\r\nContent-Type: text/html;\r\n charset=UTF-8\r\nContent-Transfer-Encoding: 7bit\r\n\r\n<html>\r\n  <body>\r\n    <h1>UserMailer#account_activation</h1>\r\n\r\n<p>\r\n  , find me in app/views/user_mailer/account_activation.html.erb\r\n</p>\r\n\r\n  </body>\r\n</html>\r\n\r\n----==_mimepart_5534d70b387f1_b853fc04845e6cc9478e--\r\n".

3 个答案:

答案 0 :(得分:0)

你可能错过了更新你的 应用程序/视图/ user_mailer文件/ account_activation.html.erb

检查RUBY ON RAILS TUTORIAL中的示例代码 代码清单10.13:帐户激活HTML视图。

答案 1 :(得分:0)

当您在rails中设置邮件程序类,使用scaffold或rake任务或其他任何内容时,它会将默认的from地址设置为“from@example.com”。此地址通常在UserMailer类的顶部指定。

答案 2 :(得分:0)

我解决了第二个问题。

检查seeds.rb文件。第一个用户的名字可能不是“Michael Example”。

我是“示例用户”。