获取 RSpec 中 html 封电子邮件的全文内容

Get full text content of html email in RSpec

如何在 rspec 中获取 html 电子邮件的全文内容作为单行字符串?

假设这是 mail.html.haml:

%p 
  This product is worth: 
  = @amount
  So buy it now!

在 rspec 我先发送电子邮件然后执行此操作:

expect(ActionMailer::Base.deliveries.last.html_part.body).to include("This product is worth: €30 So buy it now")

或:

expect(ActionMailer::Base.deliveries.last.html_part.body.raw_source).to include("This product is worth: €30 So buy it now")

或:

expect(ActionMailer::Base.deliveries.last.html_part.body.raw_source).to have_content("This product is worth: €30 So buy it now")

这些不起作用,因为 html 像这样输出到 rspec:

 + This product is worth:
 + €30
 + So buy it now!

所以我的原始代码是正确的,但我似乎无法在 rspec 中获取它。我如何将这 3 行合并为 1 行,以便我可以正确调用 include 或 have_content?

*编辑

Failure/Error:
       expect(ActionMailer::Base.deliveries.last.html_part.body.raw_source).to have_content(%q|This product is worth:
                                                                                               €30
                                                                                               So buy it now!|)

       expected        
       <p>
       This product is worth:
       €30
       So buy it now!
       Good luck!
       </p>

怎么样:

expect(ActionMailer::Base.deliveries.last.html_part.body.raw_source).to have_content(
%q|This product is worth:
€30
So buy it now|)

我不建议修改检索,因为从技术上讲,这会使规范变得无关紧要,因为匹配与呈现的内容不完全相同。你可以像上面那样分解它,这样可以避免转义内容。

如果您有很多此类测试,您可能希望将预期结果保存在某些数据存储区(db/factory/yaml 等)中并与它们进行比较。

答案很简单。 HAML 需要像这样内联:

%p This product is worth: #{@amount} So buy it now!

或者变量需要这样插值:

%p 
  This product is worth: 
  #{@amount}
  So buy it now!