URI::InvalidURIError: bad URI(is not URI?) testing Rails controllers
URI::InvalidURIError: bad URI(is not URI?) testing Rails controllers
我得到 URI::InvalidURIError
测试 Rails 家庭控制器:
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 <class:HomeControllerTest>'
堆栈如下:
Rails 5.0.0.beta3
minitest (5.8.4)
控制器测试继承自 ActionController::TestCase
,而您的测试
继承自 ActionDispatch::IntegrationTest
。所以你使用的是集成测试而不是控制器测试。
错误是:
这看起来不对,是吗? ;-)
解决方案是使用完整路径:
get '/index'
请记住,集成测试并不真正依赖于任何特定的控制器(或其他任何东西)。他们测试您应用程序中多个组件的集成。因此,如果您正在测试 UserController
的 index
操作,您可能需要使用 /users/index
.
如果您打算进行控制器测试而不是集成测试,则需要设置正确的超类。使用 get :index
(对于索引方法)应该可以正常工作。
你可以试试:
get home_index_path
而不是:
get :index
我得到 URI::InvalidURIError
测试 Rails 家庭控制器:
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 <class:HomeControllerTest>'
堆栈如下:
Rails 5.0.0.beta3
minitest (5.8.4)
控制器测试继承自 ActionController::TestCase
,而您的测试
继承自 ActionDispatch::IntegrationTest
。所以你使用的是集成测试而不是控制器测试。
错误是:
这看起来不对,是吗? ;-)
解决方案是使用完整路径:
get '/index'
请记住,集成测试并不真正依赖于任何特定的控制器(或其他任何东西)。他们测试您应用程序中多个组件的集成。因此,如果您正在测试 UserController
的 index
操作,您可能需要使用 /users/index
.
如果您打算进行控制器测试而不是集成测试,则需要设置正确的超类。使用 get :index
(对于索引方法)应该可以正常工作。
你可以试试:
get home_index_path
而不是:
get :index