首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用.png文件Ruby测试html是否包含图片标签

用.png文件Ruby测试html是否包含图片标签
EN

Stack Overflow用户
提问于 2018-07-06 03:25:11
回答 2查看 114关注 0票数 0

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-06 04:06:32

下面是来自模范红矿的assert_select_email

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

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

    # 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
票数 3
EN

Stack Overflow用户

发布于 2018-07-06 03:51:48

我不确定它是否有效,但我猜你可以做这样的事情。

expect(mail.body).to match(/.*.<img.*.png.*/)

https://github.com/rspec/rspec-expectations#regular-expressions

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51198546

复制
相关文章

相似问题

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