Rspec rails测试设计邮件

时间:2016-11-25 16:19:23

标签: ruby-on-rails email rspec devise

我试图通过devise测试rspec邮件。

这是 devise_mailer_spec.rb

要求" rails_helper"

RSpec.describe Devise::Mailer do
  it "sends a confirmation email to correct email with custom text" do
    user = FactoryGirl.create(:user, confirmed_at: nil)
    confirmation_email = Devise.mailer.deliveries.last
    expect(user.email).to eq confirmation_email.to[0]
    expect(confirmation_email.body.to_s).to have_content 'You can confirm your account email'
  end
end

但是,我收到了这个错误。

Failures:

  1) Devise::Mailer sends a confirmation email to correct email with custom text
     Failure/Error: expect(confirmation_email.body.to_s).to have_content 'You can confirm your account email'

       expected <p>Welcome adolfo@schoen.io!</p>

       <p>You can confirm your account email through the link below:</p>

       <p><a href="http://localhost:3000/users/confirmation?confirmation_token=sPMPQRe5SLDPgpakVy3J">Confirm my account</a></p>
        to respond to `has_content?`
     # ./spec/mailers/devise_mailer_spec.rb:9:in `block (2 levels) in <top (required)>'

Finished in 8 minutes 38 seconds (files took 5.02 seconds to load)
2 examples, 1 failure

Failed examples:

rspec ./spec/mailers/devise_mailer_spec.rb:4 # Devise::Mailer sends a confirmation email to correct email with custom text

我已将binding.pry设置为代码,我可以确保confirmation_email.body.to_s将抛出

"<p>Welcome edgardo@heidenreich.name!</p>\n\n<p>You can confirm your account email through the link below:</p>\n\n<p><a href=\"http://localhost:3000/users/confirmation?confirmation_token=_qi3a9rsucm6YhG8CUjt\">Confirm my account</a></p>\n"

1 个答案:

答案 0 :(得分:0)

尝试:

expect(confirmation_email.body.encoded).to include 'You can confirm your account email'