使用规范进行测试,来自导轨教程第3章的水豚无法正常工作(have_selector('title',:text =>'| Home')) [英] testing with specs, capybara from railstutorial chapter 3 does not work (have_selector('title', :text => ' | Home'))

查看:76
本文介绍了使用规范进行测试,来自导轨教程第3章的水豚无法正常工作(have_selector('title',:text =>'| Home'))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im正在研究ruby.railstutorial.org/ruby-on-rails-tutorial-book. 我正在使用Rails 3.2.7,spork,rspec,capybara,launchy和一些警卫:)

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

似乎arent的测试工作在<head> -Tag内部.如果我将<title> -tag放在<body> -tag而不是head-tag内,则效果很好. 当我将<h1>-标签放在<head>-标签内的<title>上方时,它也起作用.很奇怪,不是吗?

请帮我弄清楚.

该示例来自: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

错误消息是:

失败:

1)静态页面首页应具有标题首页" 失败/错误:page.should have_selector('title',:text =>'| Home') 水豚::期望值未达到: 预计会找到带有文本"| Home"的css"title",但没有匹配项.还找到了",它与选择器匹配,但并非全部 过滤器. #./spec/requests/static_pages_spec.rb:15:在'

中的块(3个级别)"中

那个正在工作:

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标签,以表明其工作是出于某种原因. 最后,帮助页面的主体标签中也包含标题标签,该标签也可以使用.

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

后卫的完整输出:

> 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.

解决方案

我刚刚在Github上检查了您的项目,看来您使用的是水豚边缘版本:

宝石文件

gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git'

Gemfile.lock

GIT
  remote: git://github.com/jnicklas/capybara.git
  revision: e561d249555195cdd0e9251246fc75aae876f833
  specs:
      capybara (2.0.0.beta2)
      mime-types (>= 1.16)
      nokogiri (>= 1.3.3)
      rack (>= 1.0.0)
      rack-test (>= 0.5.4)
      selenium-webdriver (~> 2.0)
      xpath (~> 1.0.0.beta1)

如果没有别的,我敢说这是造成您问题的原因. (当然,我的参考项目在更新了水豚以使用边缘版本后被破坏了)

因此,除非您热衷于帮助此处.

im working on ruby.railstutorial.org/ruby-on-rails-tutorial-book. Im using rails 3.2.7, spork, rspec, capybara, launchy and some guards :)

i have a really weird problem in Chapter 3 with testing:

It seems like the tests arent working for what is inside the <head>-Tag. If i put the <title>-tag inside the <body>-tag instead of the head-tag it works fine. Also it works when i put <h1>-tags above the <title> inside <head>-Tags. It is weird, isnt it?

Please help me figur out.

The example is from : 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

The Error-Message is:

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 '

That one is working:

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

the rendered HTML-file:

<!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>

Thanks

Edit: you can find the files im working with on github: https://github.com/farukg/sample_app/

the link to the spec file: https://github.com/farukg/sample_app/blob/master/spec/requests/static_pages_spec.rb

Explanation of what i did: The code for Home page is like it should be. The code for about page has its own layout with a h1-tag above title-tag inside to show that it works for some kind of reason. And finally the help page has its title tag inside the body tags which works too.

Im am absolutly confused, why do i have such a strange behaviour?

the complete output of 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.

解决方案

I just checked your project on Github, and it seems that you are using the edge version of capybara:

Gemfile

gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git'

Gemfile.lock

GIT
  remote: git://github.com/jnicklas/capybara.git
  revision: e561d249555195cdd0e9251246fc75aae876f833
  specs:
      capybara (2.0.0.beta2)
      mime-types (>= 1.16)
      nokogiri (>= 1.3.3)
      rack (>= 1.0.0)
      rack-test (>= 0.5.4)
      selenium-webdriver (~> 2.0)
      xpath (~> 1.0.0.beta1)

If nothing else, I would dare say this is the cause of your problem. (And sure enough, my reference project is broken all over after updating capybara to use the edge version)

So, unless you are keen to help with the beta testing of Capybara 2, you would do well to stick to the 1.1.2 version as listed here.

这篇关于使用规范进行测试,来自导轨教程第3章的水豚无法正常工作(have_selector('title',:text =&gt;'| Home'))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆