URI :: InvalidURIError:错误的URI(不是URI?)测试Rails控制器
我得到URI::InvalidURIError
测试Rails Home控制器:
require 'test_helper' class HomeControllerTest < ActionDispatch::IntegrationTest test "should get index" do get :index assert_response :success end end
得到以下错误:
E Error: HomeControllerTest#test_should_get_index: URI::InvalidURIError: bad URI(is not URI?): http://www.example.com:80index test/controllers/home_controller_test.rb:7:in `block in '
堆栈如下:
Rails 5.0.0.beta3 minitest (5.8.4)
控制器测试inheritance自ActionController::TestCase
,而您的测试inheritance自ActionDispatch::IntegrationTest
。 所以你使用的是集成测试,而不是控制器测试。
错误是:
这看起来不对,是吗? 😉
解决方案是使用完整路径:
get '/index'
请记住,集成测试并不真正依赖于任何特定的控制器(或其他任何东西)。 他们测试应用程序中几个组件的集成 。 因此,如果您正在测试UserController
的index
操作,则可能需要使用/users/index
。
如果您打算进行控制器测试而不是集成测试,则需要设置正确的超类。 使用get :index
(对于索引方法)应该可以正常工作。
你可以试试:
get home_index_path
代替:
get :index