I am at the end of chapter five doing the exercises. I am supposed to be testing that the links go to the correct pages. Here is my test code.
我在第五章结束时做了练习。我应该测试链接转到正确的页面。这是我的测试代码。
require 'spec_helper'
describe "LayoutLinks" do
it "should have the right links on the layout" do
visit root_path
click_link "About"
response.should have_selector('title', :content => "About")
click_link "Home"
response.should have_selector('title', :content => "Home")
click_link "Help"
response.should have_selector('title', :content => "Help")
click_link "Contact"
response.should have_selector('title', :content => "Contact")
click_link "Sign up now!"
response.should have_selector('title', :content => "Sign up")
end
end
Everything passes except for the last test. It says that it can not find a link with the text "Sign up now!" . I know that the page does have a "Sign up now!" link. I thought that maybe it rendered differently in the source code, but when I look at the source code it looks normal <a href="/signup" class="signup_button round">Sign up now!</a>
. From my understanding, it is supposed to click on the links and then test if the title matches the :content symbol. Am I misunderstanding something?
除了最后一次测试外,一切都通过了。它说无法找到文本“立即注册!”的链接。 。我知道该页面确实有“立即注册!”链接。我认为它可能在源代码中呈现不同,但当我查看源代码时,它看起来很正常立即注册!。根据我的理解,它应该点击链接,然后测试标题是否匹配:内容符号。我误会了什么吗?
here is the error I am getting:
这是我得到的错误:
Failures:
1) LayoutLinks should have the right links on the layout
Failure/Error: click_link "Sign up now!"
Webrat::NotFoundError:
Could not find link with text or title or id "Sign up now!"
3 个解决方案
#1
9
I believe the problem is that the "Sign up now!" link is in fact not on all pages, and this test actually navigates the pages.
我认为问题在于“立即注册!”事实上,链接并非在所有页面上都有,并且此测试实际上导航了页面。
At least in the version of this tutorial that I ran through some time ago, that link was only on the home page.
至少在我之前运行的本教程版本中,该链接仅在主页上。
If you add a visit root_path
just before that last click_link
it'll likely work.
如果您在最后一次click_link之前添加访问root_path,它可能会起作用。
A better test would have that check in an independent test related specifically to the home page.
更好的测试将检查与主页相关的独立测试。
#2
0
Also, the title text is "Sign Up"
. Capital 'U'.
此外,标题文本是“注册”。资本'U'。
The line needs to be:
该行必须是:
response.should have_selector('title', :content => "Sign Up")
The prior answer's advice to do a visit root_path
is unneeded if the page visited just before this one is "Home"
如果在此之前访问的页面是“Home”,则不需要先前的答案建议访问root_path
#3
0
The reason the test will not work is because you need to run:
测试不起作用的原因是因为您需要运行:
$ rake db:test:prepare
At least, that was why my tests were not passing.
至少,这就是为什么我的测试没有通过。
I found the solution in this forum
我在这个论坛找到了解决方案
#1
9
I believe the problem is that the "Sign up now!" link is in fact not on all pages, and this test actually navigates the pages.
我认为问题在于“立即注册!”事实上,链接并非在所有页面上都有,并且此测试实际上导航了页面。
At least in the version of this tutorial that I ran through some time ago, that link was only on the home page.
至少在我之前运行的本教程版本中,该链接仅在主页上。
If you add a visit root_path
just before that last click_link
it'll likely work.
如果您在最后一次click_link之前添加访问root_path,它可能会起作用。
A better test would have that check in an independent test related specifically to the home page.
更好的测试将检查与主页相关的独立测试。
#2
0
Also, the title text is "Sign Up"
. Capital 'U'.
此外,标题文本是“注册”。资本'U'。
The line needs to be:
该行必须是:
response.should have_selector('title', :content => "Sign Up")
The prior answer's advice to do a visit root_path
is unneeded if the page visited just before this one is "Home"
如果在此之前访问的页面是“Home”,则不需要先前的答案建议访问root_path
#3
0
The reason the test will not work is because you need to run:
测试不起作用的原因是因为您需要运行:
$ rake db:test:prepare
At least, that was why my tests were not passing.
至少,这就是为什么我的测试没有通过。
I found the solution in this forum
我在这个论坛找到了解决方案