首页 > 解决方案 > NoMethodError: nil:NilClass 的未定义方法“body”(测试失败)

问题描述

我得到一个失败(在 premailer-rails 中)这样说:

ActionMailer::Base delivery delivers email with inlined CSS
     Failure/Error: body = mail.html_part.body.to_s

     NoMethodError:
       undefined method `body' for nil:NilClass
     # ./spec/integration/delivery_spec.rb:9:in `block (2 levels) in <top (required)>'

这是spec/integration/delivery_spec.rb

require 'spec_helper'

describe 'ActionMailer::Base delivery' do
  it 'delivers email with inlined CSS' do
    WelcomeMailer.welcome_email("world").deliver_now

    mail = ActionMailer::Base.deliveries.last
    expect(mail).to be_present
    body = mail.html_part.body.to_s
    expect(body).to be_present
    expect(body).to include(%{<p style="font-size: 12px;">Hello world</p>})
  end
end

不确定发生了什么;为什么会出现 NoMethodError?:/

标签: ruby-on-railsrubyrubygemsruby-on-rails-5

解决方案


html_part如果您发送多部分电子邮件,则设置,您可以mail.body.to_s改为调用,它应该设置。


推荐阅读