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

时间:2015-09-30 19:57:53

标签: ruby-on-rails ruby railstutorial.org

我是Ruby on Rails的新手。我有一个失败的password_reset_test(Hartls的Ruby on Rails教程10.48)。到目前为止我有:

1)将我的代码与github中的源代码进行比较

2)在这里阅读有关迷你测试记者的文档:http://docs.seattlerb.org/minitest/Minitest/Assertions.html

3)将我的问题与我在stackoverflow上找到的类似问题进行了比较:Password Reset Test failing in M.Hartl's Ruby on Rails Tutorial (3rd edition), Chapter 10 (10.54)

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

到目前为止,我已经找到了错误消息的含义。在此先感谢,这个noobie非常感谢您的时间和帮助!

1 个答案:

答案 0 :(得分:0)

password_reset测试的最后一行应为

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

而不是assert_equal

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