我只是编写了一些测试,我想确保我可以测试页面上是否有一些元标记。
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
最佳答案
假设您使用的是 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
关于ruby-on-rails - RSpec Rails 找不到我的元标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30579457/