Rails控制器集成测试-预期响应为<2XX:成功>,但为<500:内部服务器错误>

问题描述 投票:-1回答:1

在红宝石'2.5.7'和护栏'5.2.3'中工作。

我无法通过控制器测试的“获取索引”测试(以下代码)。我不确定是什么问题,因为另一个测试“开始”工作正常。

这是我的控制器测试文件:

class HomeControllerTest < ActionDispatch::IntegrationTest

  test "should get index" do
    get home_path
    assert_response :success
  end

  test "should get about" do
    get about_path
    assert_response :success
  end

end

这是运行“ rails test”后的结果:

HomeControllerTest
  test_should_get_about                                           PASS (0.03s)

HomeControllerTest
  test_should_get_index                                           FAIL (0.13s)
Minitest::Assertion:         Expected response to be a <2XX: success>, but was a <500: Internal Server Error>
        test/controllers/home_controller_test.rb:7:in `block in <class:HomeControllerTest>'

如果有帮助,这是我在config文件夹中的routes.rb文件:

Rails.application.routes.draw do

  get 'home', to: 'home#index', as: :home
  get 'home/about', to: 'home#about', as: :about

end

这是我的home_controller.rb文件:

class HomeController < ApplicationController
    def index
    end

    def about
    end

end

最后,我的views目录中包含以下文件:

  • home / about.html.erb
  • home / index.html.erb

我已经尝试了很多方法,但似乎都无法解决此问题。我主要困惑为什么“ about”测试无法正常工作,而“ index”测试却无法正常工作(即使它们都是以类似的方式处理的...)

谢谢!

ruby-on-rails ruby ruby-on-rails-5
1个回答
0
投票

我刚刚意识到自己的愚蠢错误😅

谢谢@Vasilisa向我指出正确的方向!

在“ home / index.html.erb”(在我的views目录中)中,我意外地引用了另一个未在“ routes.rb”文件中列出的路径。将其从“ home / index.html.erb”中删除即可立即解决该问题。这解释了为什么我的“关于”测试有效但我的“索引”测试无效...

((以前我不知道控制器测试会关心View的内容-尤其是当我的控制器测试是如此基础时。)

© www.soinside.com 2019 - 2024. All rights reserved.