BBJ*_*BJ3 12 ruby tdd controller ruby-on-rails minitest
我URI::InvalidURIError
测试Rails Home控制器:
require 'test_helper'
class HomeControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
get :index
assert_response :success
end
end
Run Code Online (Sandbox Code Playgroud)
得到以下错误:
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>'
Run Code Online (Sandbox Code Playgroud)
堆栈如下:
Rails 5.0.0.beta3
minitest (5.8.4)
Run Code Online (Sandbox Code Playgroud)
Mar*_*oij 23
ActionController::TestCase
在您的测试继承时,控制器测试继承自ActionDispatch::IntegrationTest
.所以你使用的是集成测试,而不是控制器测试.
错误是:
这看起来不对,是吗?;-)
解决方案是使用完整路径:
get '/index'
Run Code Online (Sandbox Code Playgroud)
请记住,集成测试并不真正与任何特定的控制器(或其他任何东西)相关联.他们测试应用程序中几个组件的集成.所以,如果你正在测试的index
一个动作UserController
你可能会需要使用/users/index
.
如果您打算进行控制器测试而不是集成测试,则需要设置正确的超类.使用get :index
(对于索引方法)应该可以正常工作.