Rails 测试密码重置失败 --- 预期 +++ 实际 @@ -1 +1,40 @@

Rails test password reset failure --- expected +++ actual @@ -1 +1,40 @@

我对 Rails 上的 Ruby 完全陌生。我有一个失败的 password_reset_test(Rails 教程 10.48 上的 Hartls'Ruby)。到目前为止,我有:

1) 将我的代码与 github

中的源代码进行比较

2) 在此处阅读有关小型测试记者的文档:http://docs.seattlerb.org/minitest/Minitest/Assertions.html

3) 将我的问题与我在 Whosebug 上发现的类似问题进行比较:

4) 插入 byebug 看看我是否能理解哪里出错了

这是失败的测试

 FAIL["test_password_reset", UserMailerTest, 2015-09-12      04:04:59 +0000]
 test_password_reset#UserMailerTest (1442030699.96s)
     --- expected
    +++ actual
    @@ -1 +1,40 @@
    -"michael%40example.com"
    +"\r
    +----==_mimepart_560b45e51fccd_2eb1adb3302011d\r
    +Content-Type: text/plain;\r
    + charset=UTF-8\r
    +Content-Transfer-Encoding: 7bit\
    +\r
    +\r
    +\r
    +To reset your password click on the link below:\r
    +\r
    +http://example.com/password_resets/wAd2d-u4_dQlm4kdlhzRWA/edit?email=michael%40example.com\r
    +\r
    +This link will expire in two hours.\r
    +\r
    +If you did not request your password to be reset, please ignore this email and your password will stay as it is.\r
    +\r
    +----==_mimepart_560b45e51fccd_2eb1adb3302011d\r
    +Content-Type: text/html;\r
    + charset=UTF-8\r
    +Content-Transfer-Encoding: 7bit\r
    +\r        
    +<a href=\"http://example.com/password_resets/wAd2d-u4_dQlm4kdlhzRWA/edit?email=michael%40example.com\">Reset password</a>\r
    +\r
    +</p>This link will expire in two hours.</p>\r
    +\r
    +<p>\r
    +  If you did not request your password to be reset, please ignore this email and your password will stay as it is.\r
    +</p>\r
    +\r
    +  </body>\r
    +</html>\r
    +\r
    +----==_mimepart_560b45e51fccd_2eb1adb3302011d--\r
    +"
    test/mailers/user_mailer_test.rb:24:in `block in <class:UserMailerTest>'

这里是user_mailer_test.rb:

class UserMailerTest < ActionMailer::TestCase

  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
    assert_match user.reset_token,        mail.body.encoded
    assert_equal CGI::escape(user.email), mail.body.encoded
  end

这里是user_mailer.rb

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

这是来自 models/user.rb

    # Sets the password reset attributes
    def create_reset_digest
       self.reset_token = User.new_token
       update_attribute(:reset_digest, User.digest(reset_token))
       update_attribute(:reset_sent_at, Time.zone.now)
    end

    # Sends password reset email.
    def send_password_reset_email
        UserMailer.password_reset(self).deliver_now
    end

到目前为止,我已经了解了错误消息的含义。提前致谢,这位菜鸟非常感谢您的宝贵时间和帮助!

你的 password_reset 测试的最后一行应该是

assert_match CGI::escape(user.email), mail.body.encoded

而不是 assert_equal

该行应该是在正文中查找电子邮件地址,而不是检查电子邮件地址是否等于整个正文。