URI::InvalidURIError: bad URI(is not URI?) testing Rails controllers - ruby-on-rails

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)

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.

You can try:
get home_index_path
instead of:
get :index

Related

Ruby on Rails - RSpec test for direct route

I have a direct route like so:
direct :homepage do
"http://www.rubyonrails.org"
end
And I am trying to test this but I am not sure how. This is what I have:
describe 'redirect' do
it 'directs to homepage', type: :request do
get :homepage
expect(response).to redirect_to('http://www.rubyonrails.org')
end
end
But this fails:
1) redirect directs to homepage
Failure/Error: get :homepage
URI::InvalidURIError:
bad URI(is not URI?): "http://www.example.com:80homepage"
I have the url_helpers included in my Rspec config.
How do I test a direct route?
See: https://guides.rubyonrails.org/routing.html#direct-routes
As soon as I wrote this example, it became clear.
describe 'redirect' do
it 'directs to homepage' do
expect(homepage_url).to eq('http://www.rubyonrails.org')
end
end
I was writing my test in request specs. This works.

Testing against engine routes in Rails with Test::Unit

I am trying to make some functional tests against new functionality in an application that uses Spree.
The problem that I am running into is that when I launch my created test, the path variables that should exists are not there for the test to execute.
Say I have these routes in my local routes.rb:
Spree::Core::Engine.routes.draw do
namespace :admin do
resources :image_games
end
end
Now in my test I would have the following:
require 'test_helper'
module Spree
module Admin
class ImageGamesControllerTest < ActionController::TestCase
test 'Should get index' do
get admin_image_games_path
assert_response :success
end
end
end
end
What happens is that a Minitest::UnexpectedError: NameError: undefined local variable or methodadmin_image_games_path'` error will be thrown.
I found some advice about this but all of the solutions seem to be for RSpec. I would like to keep using Test::Unit as I feel more at home with it.
So my question, how to load in routes from a foreign Rails Engine into functional tests?
EDIT:
It seems that just the helpers are not working for my tests.
Using get '/store/admin/image_games' works but get admin_image_games_path does not. When running the application normally then these helpers do work tough.

Rails minitest syntax for get :index

I am reading the Hartl tutorial and I have a question about whether or not this is the right syntax. How does rails know what controller to look in? I have 10 controllers and I am writing a test for static pages controller. How does it know what get :home refers to and where? Does it look in the associated controller with the file name? Or does it look in routes?
Also, is assert_response a rack test or a rails test?
require 'test_helper'
class StaticPagesControllerTest < ActionController::TestCase
test "should get home" do
get :home
assert_response :success
end
test "should get help" do
get :help
assert_response :success
end
end
How does it know what get :home refers to and where?
Rails does a lot of magic, some of this magic falls into the category of convention over configuration.
In this case, the fact that the name of this test's class is StaticPagesControllerTest tells the test suite that it is testing the StaticPagesController.
It is standard rails convention to name tests this way. You could technically configure any test to look at any controller or action you want, but following this convention has many benefits (another discussion).
When you tell it to get :home it is using the get http method on the controller action named :home.
Also, is assert_response a rack test or a rails test?
It asking rails for the status code received. In this case it is checking to see if the status code was 200.
Additional info: http://api.rubyonrails.org/classes/ActionDispatch/Assertions/ResponseAssertions.html#method-i-assert_response

Rspec tests passing without actions being implemented

Hi there am working on a rails project, using a vagrant box(with ubuntu server fully configured with passenger, trying a hand at developing on a production like environment because this is my target production environment).
My problem though is that when i run "rails g controller people" and i write a simple controller spec like;
require 'spec_helper'
describe PeopleController do
describe "GET #index" do
it "responds with success" do
get :index
expect(response).to be_success
end
end
end
and i run it with "rspec spec/controllers/", the test passes right away even without having created an index action in the controller.
What do you think could be causing this?
Thank you
Is there an index.html.erb in app/views/people? If so Rails implicitly creates an action for you that renders the template.

Functional testing with Warden?

I'm trying to do functional testing and need to login with Warden.
I have done:
class ActionController::TestCase
include Warden::Test::Helpers
end
My test case is simple:
def test_access_admin_as_superuser
login_as(Person.make(:superuser))
get :index
assert_response :success
assert_nil flash[:warning]
end
I do a login_as(user) and it seems to work except when I do a get '/admin' it will fail with:
1) Error:
test_access_admin_as_superuser(AdminControllerTest):
NoMethodError: undefined method `user' for nil:NilClass
/home/silas/.rubygems/gems/gems/rails_warden-0.5.2/lib/rails_warden/controller_mixin.rb:21:in `current_user'
app/controllers/application_controller.rb:100:in `require_user'
test/functional/admin_controller_test.rb:20:in `test_access_admin_as_superuser'
any ideas?
Devise has helpers to deal with this, which is of little help if you're just using Warden. The reason things break is because action controller sets things up in the test environment in a different way. I put together a little gem that provides a module you can include for working with warden with rails in test mode: github.com/ajsharp/warden-rspec-rails.

Resources