Rails Tutorial静态页面路由测试红色(5.28)

时间:2016-07-01 14:45:27

标签: ruby-on-rails ruby railstutorial.org ruby-on-rails-4.2

做迈克尔·哈特尔的Rails教程,但是在代码清单5.28(得到RED测试而不是GREEN)上,改变测试以匹配新路由。

所有网页的错误(/,/ about,/ contact,/ help):

ActionController::UrlGenerationError: No route matches {:action=>"/*", :controller=>"static_pages"}

的routes.rb

Rails.application.routes.draw do
  root 'static_pages#home'
  get  '/help',    to: 'static_pages#help'
  get  '/about',   to: 'static_pages#about'
  get  '/contact', to: 'static_pages#contact'
end

测试/控制器/ static_pages_controller_test.rb

require 'test_helper'

class StaticPagesControllerTest < ActionController::TestCase
  test "should get home" do
    get root_path
    assert_response :success
    assert_select "title", "Ruby on Rails Tutorial Sample App"
  end

  test "should get help" do
    get help_path
    assert_response :success
    assert_select "title", "Help | Ruby on Rails Tutorial Sample App"
  end

  test "should get about" do
    get about_path
    assert_response :success
    assert_select "title", "About | Ruby on Rails Tutorial Sample App"
  end

  test "should get contact" do
    get contact_path
    assert_response :success
    assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
  end

end

static_pages_controller.rb

class StaticPagesController < ApplicationController
  def home
  end

  def help
  end

  def about
  end

  def contact
  end

end

如果您需要查看任何其他代码,请告诉我们!尝试添加为:&#39; *&#39;每次获得路线后,但无济于事。

不确定它是否是ruby / rails版本问题,但我在IDE上使用Rails 4.2.2和Ruby 2.3.0,但是&#34; rails test&#34; (正如Hartl指示使用的那样)不会工作(踢回&#34;未找到测试命令&#34;)。不确定这是否暗示更大的问题或不相关。提前谢谢!

编辑:使用这些路径的链接(如下所示)正确呈现,只是测试失败。

<%= link_to "Home",   root_path %>
<%= link_to "Help",   help_path %>

1 个答案:

答案 0 :(得分:3)

test / controllers / static_pages_controller_test.rb 有问题。我建议用以下内容替换它。

require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest

  test "should get home" do
    get root_path
    assert_response :success
    assert_select "title", "Ruby on Rails Tutorial Sample App"
  end

  test "should get help" do
    get help_path
    assert_response :success
    assert_select "title", "Help | Ruby on Rails Tutorial Sample App"
  end

  test "should get about" do
    get about_path
    assert_response :success
    assert_select "title", "About | Ruby on Rails Tutorial Sample App"
  end

  test "should get contact" do
    get contact_path
    assert_response :success
    assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
  end
end

按照我的建议更改上述文件并运行$ rails test您可以看到绿色测试。我还建议你将你的应用程序升级到rails 5.0.0,因为Michael Hartl已经将他的rails tutorials升级到rails 5.0.0以后你还有更多要学习的内容,如果你升级它,你的学习之旅将是更多错误自由和愉快。