我已完成练习以添加联系人页面,但测试在页面标题上失败。
这是我的测试文件:
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>'
请注意
真的不确定为什么它会在我紧跟教程之后回复此问题。我想在教程中取得进步,但如果我无法解决这个基本的测试问题,我不确定我会走得很远!
答案 0 :(得分:0)
我认为您可能会尝试将行get static_pages_about_url
(test "should get contact" do
下)替换为:
get static_pages_contact_url
当您的测试调用错误的网址(about
而不是contact
)时,会发生什么情况,导致检查<title>
时出错。
答案 1 :(得分:0)
请检查此代码块第二行的代码。
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
您已经提供了一个测试用例来检查关于网址的联系页面标题,这显然会导致测试失败。
您应该在上面的联系人网址上测试联系人页面标题。
做出改变,你应该开始!
也是一个动力的词,即使现在没有意义的东西也要继续前进,因为它们会在以后发生。干杯:)