繁体   English   中英

Hartl Rails教程-第3章

[英]Hartl Rails Tutorial - Chapter 3

我正在完成添加“联系人”页面的练习,但是页面标题的测试失败。

这是我的测试文件:

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>'

另请注意

  • 该页面正确显示,并带有预期的页面标题(联系方式不是关于)
  • 我使用一个全新的页面再次进行了测试,但结果相同,页面标题中返回“关于”

真的不确定在我密切关注Tutorial时为什么会返回此错误。 我想在Tutorial中取得进步,但是如果我不能解决这个基本的测试问题,我不确定我会走得很远!

我认为您可以尝试将get static_pages_about_url行(在test "should get contact" do )替换为:

get static_pages_contact_url

发生的情况是您的测试调用了错误的URL( about ,而不是contact ),从而在检查<title>时导致错误。

请在此代码块的第二行中检查您的代码。

  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上的联系页面标题,这显然会使测试失败。

您应该像上面那样在联系人url上测试联系人页面标题。

进行更改,您就该走了!

这也是一个动机,即使事情现在没有意义,只要继续下去就继续前进。 干杯:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM