电子邮件规范与Rails中的正文内容不匹配

pic*_*rdo 3 email rspec ruby-on-rails email-spec

我正在使用email_spec gem来测试一个简单的电子邮件,但由于某种原因,正文内容似乎是空的:

  1) ContactMailer welcome email to new user renders the body
     Failure/Error: mail.should have_body_text("Hi")
       expected the body to contain "Hi" but was ""
     # ./spec/mailers/contact_mailer_spec.rb:17:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)

其他每个例子都过去了 调用模板文件welcome_email.text.erb.不确定为什么身体不匹配,但电子邮件在发送时确实有一个正文.

编辑:Rspec代码是:

let(:mail) { ContactMailer.welcome_email(email) }


it "renders the body" do
  mail.should have_body_text("Hi")
end
Run Code Online (Sandbox Code Playgroud)

Mat*_*een 6

我发现这样做的最好方法是:

it "contains a greeting" do
  mail.html_part.body.should match /Hi/
end
Run Code Online (Sandbox Code Playgroud)

如果要检查多部分消息的纯文本部分,也可以使用text_part它代替html_part.

另请注意,其他人可能会建议使用#encoded,但我在使用长URL时遇到问题,因为它们可能在编码过程中被换行.