RSpec Rails不会找到我的元标记

时间:2015-06-01 17:20:45

标签: ruby-on-rails rspec meta-tags

我只是写了几个测试,我想确保我可以测试一些元标记在页面上。

describe "article", :type => :feature do
  it "has correct meta tags", js: true do
    love_article = FactoryGirl.create(:love_article, title: 'Kitten Takes Over the World!', created_at: "Sat, 30 May 2015 12:12:21 -0400", updated_at: "Mon, 01 Jun 2015 12:12:44 -0400")
    visit "/articles/#{love_article.slug}"
    page.source.should have_selector('title', text: 'Kitten Takes Over the World!', visible: false) # this is found 
    expect(page).to have_selector('pubdate', text: '05/30/15 12:12:21 PM', visible: false) # This is not found
    # ERROR 
    # page.source.should have_selector('pubdate', text: '5/30/15 12:12:21 PM', visible: false)
    # RSpec::Expectations::ExpectationNotMetError: expected to find css "pubdate" with text "5/30/15 12:12:21 PM" but there were no matches from /Users/moiseszaragoza/.rvm/gems/ruby-2.1.2/gems/rspec-expectations-3.0.4/lib/rspec/expectations/fail_with.rb:30:in `fail_with'
  end
end

现在当我添加一个调试器时,我page.source 我得到了

    <!DOCTYPE html>
<html>

<head>\n
    <title>Kitten Takes Over the World!</title>\n
    <link rel=\ "stylesheet\" media=\ "all\" href=\ "/assets/application.css\">\n


    <meta content=\ "Kitten Takes Over the World!\" property=\ "og:title\">\n
    <meta content=\ "http://127.0.0.1:51840/articles/2015/05/30/kitten-takes-over-the-world\" property=\ "og:url\">\n
    <meta content=\ "Tell on Me is enabling parents to phone in complaints on unsuspecting teen drivers.\" property=\ "og:description\">\n
    <meta content=\ "/uploads/test/article/hero_image/42/test-image-file-employee.png\" property=\ "og:image\">\n
    <meta content=\ "article\" property=\ "og:type\">\n
    <meta content=\ "IE=edge,chrome=1\" http-equiv=\ "X-UA-Compatible\">\n
    <meta charset=\ "utf-8\">\n
    <meta content=\ "text/html\" http-equiv=\ "Content-Type\">\n
    <meta name=\ "section\">\n
    <meta content=\ "05/30/15 12:12:21 PM\" property=\ "og:pubdate\">\n
    <meta content=\ "05/30/15 12:12:21 PM\" name=\ "pubdate\">\n
    <meta content=\ "06/01/15 12:12:44 PM\" name=\ "lastmod\">\n
    <meta content=\ "Joe Shmoe\" name=\ "author\">\n

我想澄清我需要从源代码中找到这一行

<meta content=\ "05/30/15 12:12:21 PM\" name=\ "pubdate\">\n

1 个答案:

答案 0 :(得分:2)

假设您正在使用Capybara,您可以执行this post中描述的内容:

page.should have_css 'meta[name="description"]', :visible => false

page.find 'meta[name="description"]', :visible => false

默认情况下,Capybara对用户不直接看到的元素不起作用(标题在浏览器/选项卡的顶部可见,因此正在传递)。

检查内容:

expect(page).to have_css 'meta[name="description"][content="expected_content"]', :visible => false