我正在关注Rails 3的Ruby on Rails教程,位于此处:
我在本教程的这一部分:
http://ruby.railstutorial.org/chapters/filling-in-the-layout#sec:integration_tests
此时我的所有网页都按预期呈现;例如,我可以点击http://localhost:3000/about
并查看示例应用程序的about页面,其中包含预期的标题(“Ruby on Rails Tutorial Sample App | About”)。然而,本节的集成测试仍然失败。
这是我的layout_links_spec.rb:
require 'spec_helper'
describe "LayoutLinks" do
it "should have a Home page at '/'" do
get '/'
response should have_selector('title', :content => "Home")
end
it "should have a Contact page at '/contact'" do
get '/contact'
response should have_selector('title', :content => "Contact")
end
it "should have an About page at '/about'" do
get '/about'
response should have_selector('title', :content => "About")
end
it "should have a Help page at '/help'" do
get '/help'
response should have_selector('title', :content => "Help")
end
end
当我使用rspec spec/
或autotest
运行测试时,我会遇到以下测试失败:
........FFFF
Failures:
1) LayoutLinks should have a Home page at '/'
Failure/Error: response should have_selector('title', :content => "Home")
expected following output to contain a <title>Home</title> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>LayoutLinks</p></body></html>
# ./spec/requests/layout_links_spec.rb:7:in `block (2 levels) in <top (required)>'
2) LayoutLinks should have a Contact page at '/contact'
Failure/Error: response should have_selector('title', :content => "Contact")
expected following output to contain a <title>Contact</title> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>LayoutLinks</p></body></html>
# ./spec/requests/layout_links_spec.rb:12:in `block (2 levels) in <top (required)>'
3) LayoutLinks should have an About page at '/about'
Failure/Error: response should have_selector('title', :content => "About")
expected following output to contain a <title>About</title> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>LayoutLinks</p></body></html>
# ./spec/requests/layout_links_spec.rb:17:in `block (2 levels) in <top (required)>'
4) LayoutLinks should have a Help page at '/help'
Failure/Error: response should have_selector('title', :content => "Help")
expected following output to contain a <title>Help</title> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>LayoutLinks</p></body></html>
# ./spec/requests/layout_links_spec.rb:22:in `block (2 levels) in <top (required)>'
Finished in 0.23497 seconds
12 examples, 4 failures
Failed examples:
rspec ./spec/requests/layout_links_spec.rb:5 # LayoutLinks should have a Home page at '/'
rspec ./spec/requests/layout_links_spec.rb:10 # LayoutLinks should have a Contact page at '/contact'
rspec ./spec/requests/layout_links_spec.rb:15 # LayoutLinks should have an About page at '/about'
rspec ./spec/requests/layout_links_spec.rb:20 # LayoutLinks should have a Help page at '/help'
...请注意,测试失败中显示的响应似乎表明我正在获得模拟/存根响应。 我认为这是我的问题,但我不知道为什么会这样。
最后,这是我的环境:
Gatito:sample_app abrown $ gem list
*** LOCAL GEMS ***
abstract (1.0.0)
actionmailer (3.0.9)
actionpack (3.0.9)
activemodel (3.0.9)
activerecord (3.0.9)
activeresource (3.0.9)
activesupport (3.0.9)
arel (2.0.10)
autotest (4.4.6)
autotest-fsevent (0.2.4)
autotest-growl (0.2.9)
autotest-rails-pure (4.1.2)
builder (2.1.2)
bundler (1.0.15)
configuration (1.2.0)
diff-lcs (1.1.2)
erubis (2.6.6)
heroku (2.3.3)
i18n (0.5.0)
launchy (0.4.0)
mail (2.2.19)
mime-types (1.16)
nokogiri (1.4.6)
polyglot (0.3.1)
rack (1.2.3)
rack-mount (0.6.14)
rack-test (0.5.7)
rails (3.0.9)
railties (3.0.9)
rake (0.9.2, 0.8.7)
rdoc (3.6.1)
rest-client (1.6.3)
rspec (2.6.0)
rspec-core (2.6.4)
rspec-expectations (2.6.0)
rspec-mocks (2.6.0)
rspec-rails (2.6.1)
sequel (3.20.0)
sinatra (1.0)
spork (0.9.0.rc8)
sqlite3 (1.3.3)
sqlite3-ruby (1.3.3)
sys-uname (0.8.5)
taps (0.3.23)
term-ansicolor (1.0.5)
thor (0.14.6)
treetop (1.4.9)
tzinfo (0.3.28)
webrat (0.7.1)
ZenTest (4.5.0)
答案 0 :(得分:1)
我明白了。我自己打了上面的测试。当我直接从教程中重新复制/粘贴测试时,他们开始传递。我很神秘,因为一切看起来都一致。但在运行diff
之后,我意识到每次测试都输入了:
response should
...但我应该打字:
# Note the "."
response.should
对每个测试进行校正后,它们都按预期通过。
答案 1 :(得分:1)
我遇到了完全相同的错误,但原因是忘记在测试中包含render_views
行:
require 'spec_helper'
describe PagesController do
render_views
describe "GET 'home'" do
it "should be successful" do
get 'home'
response.should be_success
end
end