如何测试html是否包含带有.png文件Ruby的图像标记?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (78)

我的RubyonRails Web应用程序向客户端发送了一封受欢迎的电子邮件,我想编写一个测试,以确保电子邮件包含一个标签,其中src是一个.png文件。我看到了assert_select可能会做我想做的事,但是因为我的电子邮件是Mail::Message类,我只能从电子邮件中获取html。提前感谢您的帮助!

提问于
用户回答回答于

这是assert_select_email

assert_select_email do
  assert_select 'a[href^=?]', 'http://localhost:3000/settings'
end

所有Rails程序员都应该学习像Redmin这样的文学作品来学习很多有用的技巧。

    # Extracts the body of an email and runs nested assertions on it.
    #
    # You must enable deliveries for this assertion to work, use:
    #   ActionMailer::Base.perform_deliveries = true
    #
    #  assert_select_email do
    #    assert_select "h1", "Email alert"
    #  end
    #
    #  assert_select_email do
    #    items = assert_select "ol>li"
    #    items.each do
    #       # Work with items here...
    #    end
    #  end
    def assert_select_email(&block)
      deliveries = ActionMailer::Base.deliveries
      assert !deliveries.empty?, "No e-mail in delivery list"

      deliveries.each do |delivery|
        (delivery.parts.empty? ? [delivery] : delivery.parts).each do |part|
          if part["Content-Type"].to_s =~ /^text\/html\W/
            root = Nokogiri::HTML::DocumentFragment.parse(part.body.to_s)
            assert_select root, ":root", &block
          end
        end
      end
    end

扫码关注云+社区

领取腾讯云代金券