17

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)
Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
Luca G. Soave
  • 12,271
  • 12
  • 58
  • 109

2 Answers2

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.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • hey, I didn't see that 'ActionDispatch::IntegrationTest' in my controller tests at all ... I'm digging into that – Luca G. Soave Mar 10 '16 at 20:31
  • 3
    ... strange, it seems that in Rails 5.0.0.beta3, scaffold generator is producing test controllers like that by default/design ... – Luca G. Soave Mar 10 '16 at 20:37
  • @LucaG.Soave Taht seems strange ... I don't really use the generator tools myself much, but maybe you're invoking it wrong? In any case, integration tests don't differ too much from controller tests, and AFAIK everything you can do in a controller test, you can do in an integration test (but not the other way around). I prefer to just use integration tests myself. – Martin Tournoij Mar 10 '16 at 20:39
  • 1
    @Carpetsmoker Integration tests have replaced TestCase in Rails 5. – ArtOfCode Jul 17 '16 at 22:34
  • `put :update, id: @product, product: @update` how to write this thing in the url form, Im getting same error – Nischay Namdev Jan 05 '17 at 18:52
  • While following the examples of a Rails 4 book on latest Rails (6.0.3.2), I bumped into these issues. I found this blog article ["Changes to test controllers in Rails 5"](https://blog.bigbinary.com/2016/04/19/changes-to-test-controllers-in-rails-5.html) to be quite useful, explaining the change of the test superclass to `ActionDispatch::IntegrationTest` and the deprecated/dropped `assigns` and `assert_template` methods and the rationale for the change. – filbranden Jul 29 '20 at 19:09
2

You can try:

get home_index_path

instead of:

get :index
aarkerio
  • 2,183
  • 2
  • 20
  • 34