【问题标题】:Rspec rails test devise mailerRspec rails 测试设计邮件
【发布时间】:2017-04-10 02:16:23
【问题描述】:

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

这里是 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"

【问题讨论】:

    标签: ruby-on-rails email rspec devise


    【解决方案1】:

    试试:

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

    【讨论】:

    • have_content 是 Capybara 匹配器,而不是 Rspec 匹配器,因此它的行为略有不同,但我认为这本身不是问题。我认为它可能是您在身体上调用的.to_s
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多