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

+2 votes
asked Sep 30, 2015 by john

I'm completely new to Ruby on Rails. I have a failing password_reset_test ( Hartls' Ruby on Rails tutorial 10.48).To date I have:

1) compared my code to the source code in github

2) read documentation about mini-test reporters here: http://docs.seattlerb.org/minitest/Minitest/Assertions.html

3) compared my problem to similar issues I found here on stackoverflow: Password Reset Test failing in M.Hartl's Ruby on Rails Tutorial (3rd edition), Chapter 10 (10.54)

4) inserted byebug to see if I could understand where it's going wrong

Here is the failed test

 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>'

Here is 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

Here is user_mailer.rb

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

And this is from the 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

Up to this point I've been find out what the error messages mean. Thanks in advance , this noobie greatly appreciates your time and help!

1 Answer

0 votes
answered Nov 8 by jason

The last line of your password_reset test should be

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

instead of assert_equal.

That line is supposed to be looking for the email address in the body, not checking to see if the email address is equal to the entire body.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.