使用规范进行测试,railstutorial 第 3 章中的 capybara 不起作用(have_selector ('title',:text => ' | Home' ))

标签 testing capybara ruby-on-rails-3.2 rspec-rails spork

我正在编写 ruby​​.railstutorial.org/ruby-on-rails-tutorial-book。 我正在使用 rails 3.2.7、spork、rspec、capybara、launchy 和一些 guards :)

我在第 3 章的测试中遇到了一个非常奇怪的问题:

似乎测试对 <head> 中的内容不起作用-标签。如果我把 <title> -<body> 内的标签-tag 而不是 head-tag 它工作正常。 当我输入 <h1> 时它也有效-<title> 上方的标签里面<head> -标签。这很奇怪,不是吗?

请帮我弄清楚。

示例来自:ruby.railstutorial.org/chapters/static-pages#code:title_test:

it "should have the right title" do
  visit '/static_pages/home'
  page.should have_selector('title',
                    :text => "Ruby on Rails Tutorial Sample App | Home")
end

错误信息是:

Failures:

1) Static pages Home page should have the title 'Home' Failure/Error: page.should have_selector('title', :text => ' | Home') Capybara::ExpectationNotMet: expected to find css "title" with text " | Home" but there were no matches. Also found "", which matched the selector but not all filters. # ./spec/requests/static_pages_spec.rb:15:in `block (3 levels) in '

那个正在工作:

it "should have the h1 'Sample App'" do
  visit '/static_pages/home'
  page.should have_selector('h1', :text => 'Sample App')
end

呈现的 HTML 文件:

<!DOCTYPE html>
<html>
<head>
  <title>Ruby on Rails Tutorial Sample App | Home</title>
  <!-- some css,js stuff -->
</head>
<body>

<h1>Sample App</h1>
<p>
  This is the home page for the
  <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
  sample application
</p>

</body>
</html>

谢谢

编辑: 你可以在 github 上找到我正在使用的文件: https://github.com/farukg/sample_app/

规范文件的链接: https://github.com/farukg/sample_app/blob/master/spec/requests/static_pages_spec.rb

我做了什么的解释: 主页的代码就像它应该的那样。 关于页面的代码有自己的布局,内部标题标签上方有一个 h1 标签,以表明它出于某种原因有效。 最后,帮助页面在 body 标签内有它的 title 标签,这也是有效的。

我很困惑,为什么我会有如此奇怪的行为?

guard的完整输出:

> Run all
Bundle already up-to-date
Running all specs
Running tests with args ["--drb", "--colour", "-f", "progress", "-r", "/home/faruk/.rvm/gems/ruby-1.9.3-p125/gems/guard-rspec-1.2.0/lib/guard/rspec/formatters/notification_rspec.rb", "-f", "Guard::RSpec::Formatter::NotificationRSpec", "--out", "/dev/null", "--failure-exit-code", "2", "spec"]...
..FFF.....Neues Fenster in aktueller Browsersitzung erstellt.
.FF..

Failures:

  1) Static pages Home page having application layout should have_selector head title 'Home'
     Failure/Error: page.should have_selector('head title',
     Capybara::ExpectationNotMet:
       expected to find css "head title" with text "Ruby on Rails Tutorial Sample App | Home" but there were no matches. Also found "", which matched the selector but not all filters.
     # ./spec/requests/static_pages_spec.rb:23:in `block (3 levels) in <top (required)>'

  2) Static pages Home page having application layout should have content 'Home'
     Failure/Error: page.should have_content("Ruby on Rails Tutorial Sample App | Home")
       expected there to be text "Ruby on Rails Tutorial Sample App | Home" in "Sample App This is the home page for the Ruby on Rails Tutorial sample application"
     # ./spec/requests/static_pages_spec.rb:30:in `block (3 levels) in <top (required)>'

  3) Static pages Home page having application layout should have css title 'Home'
     Failure/Error: page.should have_css("title", :text => "Ruby on Rails Tutorial Sample App | Home")
     Capybara::ExpectationNotMet:
       expected to find css "title" with text "Ruby on Rails Tutorial Sample App | Home" but there were no matches. Also found "", which matched the selector but not all filters.
     # ./spec/requests/static_pages_spec.rb:36:in `block (3 levels) in <top (required)>'

  4) Static pages about page with own layout should JUST have_selector head title 
     Failure/Error: page.should have_selector('head title')
     Capybara::ExpectationNotMet:
       expected to find css "head title" but there were no matches
     # ./spec/requests/static_pages_spec.rb:86:in `block (3 levels) in <top (required)>'

  5) Static pages about page with own layout should have_selector head title 'About Us'
     Failure/Error: page.should have_selector('head title',
     Capybara::ExpectationNotMet:
       expected to find css "head title" with text "Ruby on Rails Tutorial Sample App | About Us" but there were no matches
     # ./spec/requests/static_pages_spec.rb:93:in `block (3 levels) in <top (required)>'

Finished in 0.66215 seconds
15 examples, 5 failures

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:20 # Static pages Home page having application layout should have_selector head title 'Home'
rspec ./spec/requests/static_pages_spec.rb:27 # Static pages Home page having application layout should have content 'Home'
rspec ./spec/requests/static_pages_spec.rb:33 # Static pages Home page having application layout should have css title 'Home'
rspec ./spec/requests/static_pages_spec.rb:83 # Static pages about page with own layout should JUST have_selector head title 
rspec ./spec/requests/static_pages_spec.rb:90 # Static pages about page with own layout should have_selector head title 'About Us'
Done.

> Neues Fenster in aktueller Browsersitzung erstellt.
Neues Fenster in aktueller Browsersitzung erstellt.

最佳答案

只是因为<title><head>所以它没有出现。

使用它,解决了我的问题:

page.should have_selector 'title', :visible => false

提示:您可以应用相同的方式查看其他<head>元素,例如 <meta> ,例如,确保您(并且您将继续)对 Google 友好。

关于使用规范进行测试,railstutorial 第 3 章中的 capybara 不起作用(have_selector ('title',:text => ' | Home' )),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11726316/

相关文章:

xaml - 如何对 WPF View 文件示例 SomeView.Xaml 进行单元测试

ruby-on-rails - Ruby on Rails - 测试时的 TDD 测试错误

.net - dotnet test 不会使用通配符递归搜索文件夹中的 .csproj

ruby - 如何使用 ruby​​ 在 capybara 中导航到页面顶部

ruby-on-rails - 数据库清理程序 : Clean vs truncation

mysql - Rails 如何知道 MySQL 数据库在哪里?

r - 测试 tidyverse 中的列数

css - 无法在 css 文件中找到错误

ruby-on-rails - 如何在Ruby on Rails中查找相关记录?

ruby-on-rails - 关联 has_many 计数超过 1 的 Rails 条件