1

如何在 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>
4

2 回答 2

0

怎么样:

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 等)中并与它们进行比较。

于 2018-04-26T09:05:01.660 回答
0

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

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

或者变量需要像这样插值:

%p 
  This product is worth: 
  #{@amount}
  So buy it now!
于 2018-05-02T11:35:17.393 回答