URI::InvalidURIError: bad URI(is not URI?) testing Rails controllers

南楼画角 提交于 2019-12-04 02:53:07

问题


I get URI::InvalidURIError testing Rails Home controller:

require 'test_helper'

class HomeControllerTest < ActionDispatch::IntegrationTest
  test "should get index" do
    get :index
    assert_response :success
  end
end

get the following error:

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>'

The stack is the following:

Rails 5.0.0.beta3
minitest (5.8.4)

回答1:


Controller tests inherit from ActionController::TestCase, while your test inherits from ActionDispatch::IntegrationTest. So you're using an integration test and not a controller test.

The error is:

http://www.example.com:80index

That doesn't look right, does it? ;-)

The solution is to use a full path:

get '/index'

Remember, integration tests aren't really tied to any specific controller (or anything else, for that matter). They test the integration of several components in your application. So if you're testing the index action of a UserController you'd probably need to use /users/index.

If you intended to make a controller test and not an integration test, you want to set the correct superclass. Using get :index (for the index method) should work fine then.




回答2:


You can try:

get home_index_path

instead of:

get :index


来源:https://stackoverflow.com/questions/35925504/uriinvalidurierror-bad-uriis-not-uri-testing-rails-controllers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!