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

前端 未结 2 993
庸人自扰
庸人自扰 2021-01-17 09:52

I get URI::InvalidURIError testing Rails Home controller:

require \'test_helper\'

class HomeControllerTest < ActionDispatch::IntegrationTest         


        
2条回答
  •  清酒与你
    2021-01-17 10:36

    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.

提交回复
热议问题