Ruby on rails Rails控制器集成测试-预期响应为a<;2XX:成功>;,但这是一个<;500:内部服务器错误>;
在ruby“2.5.7”和rails“5.2.3”中工作 我无法通过控制器测试的“获取索引”测试(代码如下)。我不确定问题出在哪里,因为另一个测试“四处走动”效果很好 这是我的控制器测试文件:Ruby on rails Rails控制器集成测试-预期响应为a<;2XX:成功>;,但这是一个<;500:内部服务器错误>;,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,在ruby“2.5.7”和rails“5.2.3”中工作 我无法通过控制器测试的“获取索引”测试(代码如下)。我不确定问题出在哪里,因为另一个测试“四处走动”效果很好 这是我的控制器测试文件: class HomeControllerTest < ActionDispatch::IntegrationTest test "should get index" do get home_path assert_response :success end test
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
这是my home_controller.rb文件:
class HomeController < ApplicationController
def index
end
def about
end
end
class HomeController
最后,我的视图目录中有以下文件:
- home/about.html.erb
- home/index.html.erb
谢谢大家! 我刚刚意识到我的愚蠢错误控制器和视图中的任何错误都可能导致控制器返回内部服务器错误。这是有史以来最抽象的错误。除了有东西坏了之外,我们没有别的事可做。您必须提高自己的故障排除技能,或者让我们了解路线、控制器和视图的整体情况,以便我们能够实际再现和诊断问题。您的“home/index.html.erb”中有什么内容?通过浏览器获取
/home
时是否有错误?谢谢@Vasilisa!你帮了大忙!“home/index.html.erb”确实存在问题,因为我无意中包含了一个不在“routes.rb”中的路径。我很乐意提供帮助:)我不是小型测试方面的专家(几乎总是使用Rspec),但我认为应该有一种没有视图渲染的测试。但您使用的是IntegrationTest
它测试整个应用程序交互并呈现视图
Rails.application.routes.draw do
get 'home', to: 'home#index', as: :home
get 'home/about', to: 'home#about', as: :about
end
class HomeController < ApplicationController
def index
end
def about
end
end