By filling in the code marked FILL_IN in Listing 3.42, write a test for the root route

做~自己de王妃 提交于 2019-12-24 06:10:12

问题


Adding the root route in Listing 3.41 leads to the creation of a Rails helper called root_url (in analogy with helpers like static_pages_home_url). By filling in the code marked FILL_IN in Listing 3.42, write a test for the root route.

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

What Should be the FILL_IN? I tried 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>'

回答1:


Try with:

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

More info on http://guides.rubyonrails.org/testing.html#implementing-an-integration-test




回答2:


I was able to get it to work with root_url. You might want to check that you are serving the home page on routes.rb.




回答3:


For anyone else in the future you're telling it to go to your home page which is in your static pages controller then we copy the syntax from the other tests... so it ends up being written like this...

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



回答4:


In your test you should be telling it to look for the <Home | Ruby on Rails Tutorial Sample App> since root and home will display the same.




回答5:


this is the complete solution (guarantee it will work)

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


来源:https://stackoverflow.com/questions/39113739/by-filling-in-the-code-marked-fill-in-in-listing-3-42-write-a-test-for-the-root

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!