ruby-on-rails - RSpec Rails 找不到我的元标签

标签 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

最佳答案

假设您使用的是 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/

相关文章:

angularjs - 如何为谷歌机器人动态设置 Angular 单页应用程序的标题和描述?

ruby-on-rails - 在 Rails (PostgreSQL) 中批量更新个人记录

ruby-on-rails - 从零到多关系的 ActiveRecord 简单最大日期

javascript - 从 JS 获取 URL 元数据

php - 基于 URL 生成动态元标记

ruby-on-rails - 我如何 stub results.map(& :blank_image) in Rspec?

ruby-on-rails - Postgres 适配器似乎为实际具有非 NULL 值的列返回 nil 日期

ruby-on-rails - Rails Bundle,gems 冲突,解决它的最佳方法

ruby - rspec:第一次失败后如何继续测试

ruby-on-rails - 在 Rspec 请求测试中使用 Session、Cookies 或 Current_something