【问题标题】:Getting a rails test to pass让 Rails 测试通过
【发布时间】:2016-08-16 07:51:40
【问题描述】:

按照 Rails 教程进行操作,但在稍微切线时关闭,现在无法通过测试。 下面是我的路线文件和我的测试文件。 home 测试失败,这条线 , to: "static_pages#home", as: "home" 是让它出现的线。即删除它,它会通过。我想知道为什么有人可以解释为什么会失败以及如何更改测试以使其通过但保留这条线?

我的路线文件:

Rails.application.routes.draw do

  resources :static_pages
    get 'static_pages/help'
    get 'static_pages/test'
    get 'static_pages/home', to: "static_pages#home", as: "home"
    root 'application#hello'

end

我的测试文件:

require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest
  test "should get home" do
    get static_pages_home_url
    assert_response :success
  end

  test "should get help" do
    get static_pages_help_url
    assert_response :success
  end

  test "should get test" do
    get static_pages_test_url
    assert_response :success
  end


end

【问题讨论】:

    标签: ruby-on-rails ruby testing routes


    【解决方案1】:

    static_pages_home_url 是带有控制器 static_pages 和操作 home 的路由的默认辅助方法,但您将名称设置为 home,因此您的测试应该是

    test "should get home" do
      get home_url
      assert_response :success
    end
    

    【讨论】:

    • 啊太棒了!所以我有as 关键字的地方,我只是把它改成那个。干杯!将在 5 分钟内接受答复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多