使用 Ruby on Rails 教程进行集成测试
我正在尝试遵循 ruby on Rails 教程并实施集成测试。
我首先运行命令:bundle exec rspec spec/
它告诉我所有的测试都通过了,只有一个测试通过了。这是我认为问题所在的部分:
require 'spec_helper'
describe "LayoutLinks" do
it "should have the right links on the layout" do
visit root_path
click_link "Help"
response.should have_selector('title', :content => "Help")
click_link "Contact"
response.should have_selector('title', :content => "Contact")
click_link "Home"
response.should have_selector('title', :content => "Home")
click_link "Sign up now!"
response.should have_selector('title', :content => "Sign Up")
click_link "About"
response.should have_selector('title', :content => "About")
end
end
结果我得到以下结果:
Failure/Error: response.should have_selector('title', :content => "Help")
expected following output to contain a <title>Help</title> tag:
#home.html.erb page's source shown
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Ruby on Rails Tutorial Sample App | Home</title>
#The line above is why the test fails.
#It is loading home.html.erb instead of help.html.erb
.
.
.
# ./spec/requests/layout_links_spec.rb:34:in `block (2 levels) in <top (required)>'
我可以移动测试的顺序,但失败的测试始终是最上面的测试。这让我相信这里有问题,而不是实际的 Rails 代码。我还可以访问演示网站,链接有效,并且它们会转到正确的页面。我查看了其他人遇到的其他问题,但似乎找不到遇到相同问题的人。我该如何解决这个问题?
更新:
noahc:sample_app noahc$ rake routes
users_new GET /users/new(.:format) {:controller=>"users", :action=>"new"}
signup /signup(.:format) {:controller=>"users", :action=>"new"}
contact /contact(.:format) {:controller=>"pages", :action=>"contact"}
about /about(.:format) {:controller=>"pages", :action=>"about"}
help /help(.:format) {:controller=>"pages", :action=>"help"}
/help(.:format) {:controller=>"pages", :action=>"help"}
root / {:controller=>"pages", :action=>"home"}
pages_home GET /pages/home(.:format) {:controller=>"pages", :action=>"home"}
pages_contact GET /pages/contact(.:format) {:controller=>"pages", :action=>"contact"}
pages_about GET /pages/about(.:format) {:controller=>"pages", :action=>"about"}
pages_help GET /pages/help(.:format) {:controller=>"pages", :action=>"help"}
I'm trying to follow the ruby on rails tutorial and impliment the Integration Testing.
I first run the command: bundle exec rspec spec/
And it tells me all but one of my sixteen tests passes. Here is the part where I think the issue is:
require 'spec_helper'
describe "LayoutLinks" do
it "should have the right links on the layout" do
visit root_path
click_link "Help"
response.should have_selector('title', :content => "Help")
click_link "Contact"
response.should have_selector('title', :content => "Contact")
click_link "Home"
response.should have_selector('title', :content => "Home")
click_link "Sign up now!"
response.should have_selector('title', :content => "Sign Up")
click_link "About"
response.should have_selector('title', :content => "About")
end
end
As a result I get the following:
Failure/Error: response.should have_selector('title', :content => "Help")
expected following output to contain a <title>Help</title> tag:
#home.html.erb page's source shown
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Ruby on Rails Tutorial Sample App | Home</title>
#The line above is why the test fails.
#It is loading home.html.erb instead of help.html.erb
.
.
.
# ./spec/requests/layout_links_spec.rb:34:in `block (2 levels) in <top (required)>'
I can move around the order of the tests and it is always the top test that fails. This makes me believe there is something wrong here and not with the actual rails code. I can also go to the demo website and the links work and they go to the correct pages. I have looked at the other issues other people have had with this and I can't seem to find anyone who is having the same issues. How can I go about trouble shooting this?
Update:
noahc:sample_app noahc$ rake routes
users_new GET /users/new(.:format) {:controller=>"users", :action=>"new"}
signup /signup(.:format) {:controller=>"users", :action=>"new"}
contact /contact(.:format) {:controller=>"pages", :action=>"contact"}
about /about(.:format) {:controller=>"pages", :action=>"about"}
help /help(.:format) {:controller=>"pages", :action=>"help"}
/help(.:format) {:controller=>"pages", :action=>"help"}
root / {:controller=>"pages", :action=>"home"}
pages_home GET /pages/home(.:format) {:controller=>"pages", :action=>"home"}
pages_contact GET /pages/contact(.:format) {:controller=>"pages", :action=>"contact"}
pages_about GET /pages/about(.:format) {:controller=>"pages", :action=>"about"}
pages_help GET /pages/help(.:format) {:controller=>"pages", :action=>"help"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
诺亚,我建议将该测试分成多个测试,每个测试对应您想要测试的每个标题。这将使您更容易准确地查明问题所在。像这样的东西:
等等...这将使您更容易准确地查明正在发生的情况以及您在哪里遇到问题。另外,页面源代码中显示的内容是什么?您在解释粘贴片段中实际发生的情况之前切断了错误消息...查看实际返回的代码所说的内容,看看标题是什么以及它如何/为什么与 rspec 期望的不同。
Noah, I would recommend splitting that test up into multiple tests, one for each title that you would like to test. That will make it a little easier for you to pinpoint exactly what's wrong. Something like this:
etc... This will make it easier to pinpoint exactly what is going on, and where you're experiencing a problem. Also, what did it say was being displayed in the page's source? You cut off the error message before it explained what really happened in your paste snippet... Look inside what the code that is actually being returned says to see what the title is and how/why it is differing from what rspec is expecting.
我不使用 rspec,但可能是因为 "Home" != "Ruby on Rails Tutorial Sample App | Home"
内容根本不匹配,因此测试失败。
另外,请检查源中的帮助链接,以确保它实际上位于您认为的位置......
I don't use rspec, but could be because "Home" != "Ruby on Rails Tutorial Sample App | Home"
The contents do not match at all, so the test fails.
Also, check your help link in your source to be sure it is in fact going where you think it is...