通过填写代码清单3.42中标记为FILL_IN的代码,为根路由编写一个测试

时间:2016-08-24 03:02:42

标签: ruby-on-rails-5

在清单3.41中添加根路由会导致创建名为root_url的Rails帮助程序(类似于static_pages_home_url这样的帮助程序)。通过填写代码清单3.42中标记为FILL_IN的代码,为根路由编写一个测试。

Listing 3.41: Setting the root route to the Home page.
config/routes.rb
 Rails.application.routes.draw do
  root 'static_pages#home'
end

Listing 3.42: A test for the root route. green
test/controllers/static_pages_controller_test.rb
 require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest

  test "should get root" do
    get FILL_IN
    assert_response FILL_IN
  end
end

FILL_IN应该是什么? 我试过static_pages_root_url,root_url。

rails test fails.
F

Failure:
StaticPagesControllerTest#test_should_get_root [/home/ubuntu/workspace/sample_app/test/controllers/static_pages_controller_test.rb:12]:
<Root | Ruby on Rails Tutorial Sample App> expected but was
<Home | Ruby on Rails Tutorial Sample App>..
Expected 0 to be >= 1.

E

Error:
StaticPagesControllerTest#test_should_get_root:
ArgumentError: Invalid response name: root_url
    test/controllers/static_pages_controller_test.rb:11:in `block in <class:StaticPagesControllerTest>'

5 个答案:

答案 0 :(得分:4)

尝试:

  test "should get root" do
    get '/'
    assert_response :success
  end

有关http://guides.rubyonrails.org/testing.html#implementing-an-integration-test

的更多信息

答案 1 :(得分:1)

我能够让它与root_url一起使用。您可能想要检查您是否在routes.rb上提供主页。

答案 2 :(得分:1)

对于将来的任何其他人,您都要告诉它转到您的静态页面控制器中的主页,然后我们从其他测试中复制语法...所以它最终会像这样写。 ..

html, body {
    margin: 0;
    padding: 0;
    min-width: 1200px;
    width: auto !important;
}

...

.container {
    position: relative;
}

.header
{
    position: absolute;
    height: 40px;
    width: 100%;
    z-index: 110;
    white-space: nowrap;
}

.header-left-part
{
    position: absolute;
    height: 40px;
    width: 25%;
    background: #eaaa00; /* For browsers that do not support gradients */    
    background: -webkit-linear-gradient(left, #fef9e7, #ffdb8b); /* For Safari 5.1 to 6.0 */
    background: -o-linear-gradient(right, #fef9e7, #ffdb8b); /* For Opera 11.1 to 12.0 */
    background: -moz-linear-gradient(right, #fef9e7, #ffdb8b); /* For Firefox 3.6 to 15 */
    background: linear-gradient(to right, #fef9e7, #ffdb8b); /* Standard syntax (must be last) */
    white-space: normal;
}

.header-middle-part
{
    position: absolute;
    height: 40px;
    left: 25%;
    width: 50%;
    background: #eaaa00; /* For browsers that do not support gradients */    
    background: -webkit-linear-gradient(left, #ffdb8b, #b2a000); /* For Safari 5.1 to 6.0 */
    background: -o-linear-gradient(right, #ffdb8b, #b2a000); /* For Opera 11.1 to 12.0 */
    background: -moz-linear-gradient(right, #ffdb8b, #b2a000); /* For Firefox 3.6 to 15 */
    background: linear-gradient(to right, #ffdb8b, #b2a000); /* Standard syntax (must be last) */
    text-align: center;
    font-size: 18px;
    font-weight: bold;
    white-space: normal;
}

.header-right-part
{
    position: absolute;
    height: 40px;
    left: 75%;
    width: 25%;
    background: #EAAA00; /* For browsers that do not support gradients */    
    background: -webkit-linear-gradient(left, #b2a000, #138d75); /* For Safari 5.1 to 6.0 */
    background: -o-linear-gradient(right, #b2a000, #138d75); /* For Opera 11.1 to 12.0 */
    background: -moz-linear-gradient(right, #b2a000, #138d75); /* For Firefox 3.6 to 15 */
    background: linear-gradient(to right, #b2a000, #138d75); /* Standard syntax (must be last) */
    color: white;
    white-space: normal;
}

.header-title-left-part
{
    float: left;
    margin-left: 15px;
    margin-top: 2px;
    font-size: 18px;
    font-weight: bold;
}

.header-title-middle-part-text
{
    float: left;
    width: 100%;
    height: 40px;
    margin-top: 8px;
    font-size: 18px;
    font-weight: bold;
    text-align: center;
}

.header-title-right-part-login
{
    float: right;
    margin-right: 3px;
    margin-top: 0px;
}

.title-right-main
{
    float: right;
    margin-right: 3px;
    margin-top: 1px;
}

.title-right-main-icons
{
    float: right;
    margin-right: 3px;
    margin-top: 12px;
}

答案 3 :(得分:0)

在你的测试中,你应该告诉它寻找<Home | Ruby on Rails Tutorial Sample App>,因为root和home将显示相同的内容。

答案 4 :(得分:0)

这是完整的解决方案(保证它会起作用)

  test "should get root" do
    get root_url
    assert_response :success
    assert_select "title", "Home | #{@base_title}"   
  end