RSpec梅勒测试匹配失败

时间:2012-02-10 05:47:27

标签: ruby-on-rails regex rspec actionmailer

我有以下Rspec测试:

it "Password reset instructions" do
    user = Factory(:user)
    user.send_reset_password_instructions

    email = last_email

    email.body.encoded.should match(edit_user_password_url(:reset_password_token => user.reset_password_token))
end

尝试匹配邮件程序中的edit_password_url

!!!
%html
  %head
    %meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
  %body
    %h1 Hey #{@resource.first_name}
    %p Forget your password? Its cool-- it happens to the best of us.
    %p To reset the password to your TippingPress account, click the link below:
    %p= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @resource.reset_password_token)
    %p If you didn’t request a password change, you can email us at support@tippingpress.com.
    %p Best,
    %p 
      Alex & Larson
      %br  
      = link_to "TippingPress.com", "http://www.tippingpress.com"

我收到以下错误:

  1) UserMailer Password reset instructions
     Failure/Error: email.body.encoded.should match(edit_user_password_url(:reset_password_token => user.reset_password_token))
       expected "<!DOCTYPE html>\r\n<html>\r\n<head>\r\n<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>\r\n</head>\r\n<body>\r\n<h1>Hey Tester</h1>\r\n<p>Forget your password? Its cool-- it happens to the best of us.</p>\r\n<p>To reset the password to your TippingPress account, click the link below:</p>\r\n<p><a href=\"http://localhost:3000/users/password/edit?reset_password_token=8RMhiAxsRihME1mL7JiE\">Change my password</a></p>\r\n<p>If you didn’t request a password change, you can email us at support@tippingpress.com.</p>\r\n<p>Best,</p>\r\n<p>\r\nAlex & Larson\r\n<br>\r\n<a href=\"http://www.tippingpress.com\">TippingPress.com</a>\r\n</p>\r\n</body>\r\n</html>\r\n" to match "http://localhost:3000/users/password/edit?reset_password_token=8RMhiAxsRihME1mL7JiE"

似乎两个网址匹配,我假设这是由于我缺乏REGEX或字符编码知识而导致的某种错误。提前谢谢!

1 个答案:

答案 0 :(得分:9)

实际上,你没有将正则表达式传递给match方法,而只是一个字符串。你最好这样做:

email.body.encoded.should include(edit_user_password_url(:reset_password_token => user.reset_password_token))