我正在完成添加联系人页面的练习,但是测试在页面标题上失败了。
这是我的测试文件:
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
def setup
@base_title = "Ruby on Rails Tutorial Sample App"
end
test "should get root" do
get root_url
assert_response :success
end
test "should get home" do
get static_pages_home_url
assert_response :success
assert_select "title", "Home | #{@base_title}"
end
test "should get help" do
get static_pages_help_url
assert_response :success
assert_select "title", "Help | #{@base_title}"
end
test "should get about" do
get static_pages_about_url
assert_response :success
assert_select "title", "About | #{@base_title}"
end
test "should get contact" do
get static_pages_about_url
assert_response :success
assert_select "title", "Contact | #{@base_title}"
end
end下面是contact.html.erb文件:
<% provide(:title, "Contact") %>
<h1>Contact</h1>
<p>
Contact the Ruby on Rails Tutorial about the sample app at the
<a href="http://www.railstutorial.org/contact">contact page</a>.
</p>我还完成了以下工作:
但是,我得到了以下错误消息:
test_should_get_contact#StaticPagesControllerTest (0.45s)
<Contact | Ruby on Rails Tutorial Sample App> expected but was
<About | Ruby on Rails Tutorial Sample App>..
Expected 0 to be >= 1.
test/controllers/static_pages_controller_test.rb:35:in `block in <class:StaticPagesControllerTest>'还请注意
真的不知道它为什么要返回这个,因为我已经密切关注教程。我想在教程中取得进步,但是如果我不能解决这个基本的测试问题,我不确定我会走得很远!
发布于 2017-01-20 06:41:21
请检查此代码块第二行的代码。
test "should get contact" do
# get static_pages_about_url # This is wrong correct it to as below
get static_pages_contact_url
assert_response :success
assert_select "title", "Contact | #{@base_title}"
end您已经给出了一个测试用例来检查about url上的联系人页面标题,这显然会导致测试失败。
您应该在上面的联系人网址上测试联系人页面标题。
改变一下,你就该走了!
还有一个动机,就是继续前进,即使现在的事情没有意义,因为以后会的。干杯:)
发布于 2017-01-20 06:39:53
我认为您可以尝试将行get static_pages_about_url (在test "should get contact" do下)替换为:
get static_pages_contact_url所发生的情况是,您的测试调用错误的url (about,而不是contact),导致在检查<title>时出现错误。
https://stackoverflow.com/questions/41757109
复制相似问题