2016-08-24 17 views
1

添加根路径清单3.41导致创建一个Rails助手叫root_url(按类似于像static_pages_home_url佣工)中的一个测试。通过填充代码清单3.42中标记为FILL_IN的代码,为根路由写一个测试。通过在代码填写清单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>' 
+0

这是功课? –

回答

0

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

更多信息在您的测试,你应该告诉它来寻找<Home | Ruby on Rails Tutorial Sample App>因为root和家庭将显示相同。

1

我能得到它与root_url工作。您可能想要检查您是否在routes.rb上提供主页。

1

对于你告诉它去你的主页的未来是在你的静态页面控制器别人那么我们复制的语法与其他测试...所以它最终被这样写的.. 。

test "should get root" do 
    get static_pages_home_url 
    assert_response :success 
    end 
0

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

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