首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何对Rails邮件程序生成的html内容进行单元测试?

如何对Rails邮件程序生成的html内容进行单元测试?
EN

Stack Overflow用户
提问于 2016-02-12 04:43:14
回答 2查看 816关注 0票数 2

我遇到过一些工具,它们可以更容易地测试Rails应用程序中生成的电子邮件,但它们是为集成测试(即capybara-email)而设计的。但是,我正在编写一个直接与邮件程序一起工作的单元测试。

我目前有一个对我的邮件的测试,看起来像这样:

代码语言:javascript
复制
RSpec.describe DigestMailer do
  describe "#daily_digest" do
    let(:mail) { DigestMailer.daily_digest(user.id) }
    let(:user) { create(:user) }

    it "sends from the correct email" do
      expect(mail.from).to eql ["support@example.com"]
    end

    it "renders the subject" do
      expect(mail.subject).to eql "Your Daily Digest"
    end

    it "renders the receiver email" do
      expect(mail.to).to eql [user.email]
    end

    it "renders the number of new posts" do
      expect(mail.body.raw_source).to match "5 New Posts"
    end
  end
end

但是,我希望能够比简单地使用正则表达式更容易地测试html内容。

我真正想做的事情是这样的:

代码语言:javascript
复制
within ".posts-section" do
  expect(html_body).to have_content "5 New Posts"
  expect(html_body).to have_link "View More"
  expect(find_link("View More").to link_to posts_url
end

我不知道是否有一种方法可以直接使用Capybara来实现这样的事情。也许有其他选择可以提供类似的功能?

EN

Stack Overflow用户

发布于 2019-01-10 20:39:08

根据this Thoughtbot article,您可以将字符串转换为Capybara::Node::Simple实例。这允许你使用水豚匹配器来对抗它。

我已经创建了一个帮助器方法,它使用以下代码:

代码语言:javascript
复制
def email_html
  Capybara.string(mail.html_part.body.to_s)
end

然后我可以按如下方式使用它:

代码语言:javascript
复制
expect(email_html).to have_content "5 New Posts"
票数 3
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35349650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档