我正在使用ruby.railstutorial.org/ruby-on-rails-tutorial-book。我使用轨道3.2.7,spork,rspec,水豚,洗衣店和一些警卫:)
我在第三章测试中遇到了一个非常奇怪的问题:
测试似乎不适用于<head>
-标记中的内容。如果我把<title>
-标记放在<body>
-标记中,而不是放在head标记中,它就可以正常工作。当我把<h1>
-标签放在<title>
-标签里面的<head>
-标签上面时,它也能工作。这很奇怪,不是吗?
请帮我计算一下。
示例来自:ruby.railstutorial.org/charges/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.sshould 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
我所做的解释:主页的代码应该是这样的。about页面的代码有自己的布局,其中title标记上方有一个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.
这只是因为<title>
在<head>
中,所以它不会出现。
使用这个,解决了我的问题:
page.should have_selector 'title', :visible => false
提示:您可以将同样的方法应用于检查其他<head>
元素,例如<meta>
,以确保您(并将保持)对谷歌友好。
我刚刚在Github上检查了你的项目,看起来你使用的是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)
如果没有别的,我敢说这就是你问题的原因。(果不其然,我的参考项目在更新水豚使用边缘版本后被彻底破坏了)
因此,除非你热衷于帮助Capybara 2的测试版,否则你最好坚持这里列出的1.1.2版本。
如果您使用的是Capybara 2,您可以使用have_title
方法,如下所示:
page.should have_title("Ruby on Rails Tutorial Sample App | Home")
你能用这个测试吗:
page.should have_selector('head title',
:text => "Ruby on Rails Tutorial Sample App | Home")
目前最简单的方法是从页面html访问Title元素,这将返回标题:
page.html.match(/<title>(.*)</title>/)[1].to_s
这并不理想,因为它是在html(!!)上使用正则表达式,但会起作用。可能最好写一个辅助函数
# put in spec/support/utilities.rb
def page_title(page)
page.html.match(/<title>(.*)</title>/)[1].to_s
end
那么你的测试可以写成
page_title(page).should eq( "Ruby on Rails Tutorial Sample App | Home" }
推迟了这么久,这对我来说也是新的。多亏了stackoverflow,我能够在描述之后的规范文件顶部修复一些带有render_views的测试。我通过了的测试
describe "GET 'home'" do
it "returns http success" do
get 'home'
response.should be_success
end
但在没有使用布局的情况下,我得到了以下错误:text水豚::预期未达到:期望找到文本为"Home|RubyonRails教程示例应用程序"的css"title",但没有匹配项。但除此之外,在使用gem"capybara"后,gem文件中的"1.1.2"我的测试通过了。但我在路线上也用"回家"作为动作赛。我希望这能有所帮助。