Rails integration test don't see routes - ruby-on-rails

I am writing because I recently added a two-step authorization to active_admin in my application using the gem: 'devise-two-factor'. Everything is working, unfortunately when I started to write tests I noticed that the paths I added do not exist in the test environment (there is an error). What is the reason for this and how to fix it. thanks in advance for your help.
Error:
test_can_setup_2FA_for_admin_user ERROR (325.99s)
Minitest::UnexpectedError: ActionController::RoutingError: No route matches [GET] "/admin/admin_users/6/edit_two_factor"
test/controllers/admin/sessions_controller_test.rb:11:in `block in <class:AdminUsersControllerTest>'
Finished in 325.99535s
1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
[SlowTest] AdminUsersControllerTest#test_can_setup_2FA_for_admin_user : 325.99s
routes:
rails routes | grep edit_two_factor
edit_two_factor_authentication_confirmation GET /two_factor_authentication/confirmation/edit(.:format) two_factor_authentication/confirmations#edit {:host=>"571e-77-222-242-230.eu.ngrok.io"}
edit_two_factor_authentication_recovery_code GET /two_factor_authentication/recovery_codes/:id/edit(.:format) two_factor_authentication/recovery_codes#edit {:host=>"571e-77-222-242-230.eu.ngrok.io"}
edit_two_factor_authentication GET /two_factor_authentication/edit(.:format) two_factor_authentications#edit {:host=>"571e-77-222-242-230.eu.ngrok.io"}
edit_two_factor_admin_admin_user GET /admin/admin_users/:id/edit_two_factor(.:format) admin/admin_users#edit_two_factor {:host=>"571e-77-222-242-230.eu.ngrok.io"}
test code:
class AdminUsersControllerTest < ActionDispatch::IntegrationTest
test 'can setup 2FA for admin user' do
admin_user = create(:admin_user)
get edit_two_factor_admin_admin_user_path(admin_user)
assert_template 'admin/admin_users/show_two_factor'
end
end
RAILS_ENV=test rails routes | grep edit_two_factor
edit_two_factor_authentication_confirmation GET /two_factor_authentication/confirmation/edit(.:format) two_factor_authentication/confirmations#edit {:host=>"prograils.io"}
edit_two_factor_authentication_recovery_code GET /two_factor_authentication/recovery_codes/:id/edit(.:format) two_factor_authentication/recovery_codes#edit {:host=>"prograils.io"}
edit_two_factor_authentication GET /two_factor_authentication/edit(.:format) two_factor_authentications#edit {:host=>"prograils.io"}
edit_two_factor_admin_admin_user GET /admin/admin_users/:id/edit_two_factor(.:format) admin/admin_users#edit_two_factor {:host=>"prograils.io"}

Related

Test ActionController::RoutingError: uninitialized constant V1::LocationsController

Continued from uninitialized constant error when running tests on Ruby on rails 5 ...
I'm writing test cases for my api controller with namespace - v1 but it seems ROR doesn't understand the namespace V1 exists.
When I run "rails routes", this is what I get:
v1_locations GET /v1/locations(.:format) v1/locations#index
POST /v1/locations(.:format) v1/locations#create
v1_location GET /v1/locations/:id(.:format) v1/locations#show
PATCH /v1/locations/:id(.:format) v1/locations#update
PUT /v1/locations/:id(.:format) v1/locations#update
DELETE /v1/locations/:id(.:format) v1/locations#destroy
and this is what I have in my test file...
class V1::LocationControllerTest < ActionDispatch::IntegrationTest
def setup()
# A bunch of stuff here
end
test "cannot retrieve a list of locations without a valid token" do
get v1_locations_url
assert_response :unauthorized
end
# More tests here...
end
and this is what my controller looks like
class V1::LocationController < ApplicationController
# To be filled once tests run and fail
end
and this is the error message I'm getting...
Error:
V1::LocationControllerTest#test_cannot_create_a_location_without_a_valid_token:
ActionController::RoutingError: uninitialized constant V1::LocationsController
test/controllers/v1/location_controller_test.rb:32:in `block in <class:LocationControllerTest>'
bin/rails test test/controllers/v1/location_controller_test.rb:31
It's also funny that I get page not found error when I send a GET request to /v1/locations
Mmm... What am I missing here?
Thanks,
Check your error log it says ActionController::RoutingError: uninitialized constant V1::LocationsController, while you controller name is LocationController.
Please rename it to LocationsController and rename a file to locations_controller.rb.
You can get more insights about that here

Rails minitest returning "NoMethodError: undefined method `make_response!'"

I have a couple tests that run fine independently
rails t test/controllers/briefs_controller_test.rb
Running via Spring preloader in process 24174
Run options: --seed 46330
# Running:
.................................
Finished in 9.219203s, 3.5795 runs/s, 3.9049 assertions/s.
33 runs, 36 assertions, 0 failures, 0 errors, 0 skips
But all together they produce
Running via Spring preloader in process 24023
Run options: --seed 35139
# Running:
..............................................................................................E
Error:
BriefsControllerTest#test_should_get_project_brief_by_admin:
NoMethodError: undefined method `make_response!' for ProjectsController:Class
test/controllers/briefs_controller_test.rb:9:in `block in <class:BriefsControllerTest>'
bin/rails test test/controllers/briefs_controller_test.rb:7
.....E
Error:
BriefsControllerTest#test_should_get_project_brief_by_brand:
NoMethodError: undefined method `make_response!' for ProjectsController:Class
test/controllers/briefs_controller_test.rb:15:in `block in <class:BriefsControllerTest>'
I was suspecting fixtures at first but only controllers test with get or post with project routes are crashing.
require 'test_helper'
class BriefsControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
include I18n::Alchemy
test "should get project brief by admin" do
sign_in users(:admin)
get project_brief_url(projects(:project_two).id)
assert_response :success
end
test "should get project brief by brand" do
sign_in users(:brand)
get project_brief_url(projects(:project_one).id)
assert_response :success
end
[end of test file removed for clarity ]
Any ideas ?
This only happens on routes based on projects calls on other routes are fine.

Rails Rspec Controller return 'undefined method Sinatra::Application:Class'

Typically I avoid controller tests and stick to Model & Cucumber, but i find myself needing one and I cant seem to get moving.
The following code is simplistic.. i have ripped out just about everything
1 require 'spec_helper'
2
3 describe ExperiencesController do
4 describe "GET #show" do
5 context "experience item" do
6 it "redirects to the success url" do
7 experience = build(:experience)
8 get :show, :id => experience.id
9 #should be a test here :)
10 end
11 end
12 end
13 end
Yields the following
Failures:
1) ExperiencesController GET #show experience item redirects to the success url
Failure/Error: get :show, :id => experience.id
NoMethodError:
undefined method `id' for Sinatra::Application:Class
# (__DELEGATE__):2:in `get'
# ./spec/controller_spec/experiences_controller_spec.rb:8:in `block (4 levels) in <top (required)>'
$ rake routes|grep experience
experiences GET /experiences(.:format) experiences#index
POST /experiences(.:format) experiences#create
new_experience GET /experiences/new(.:format) experiences#new
edit_experience GET /experiences/:id/edit(.:format) experiences#edit
experience GET /experiences/:id(.:format) experiences#show
PUT /experiences/:id(.:format) experiences#update
DELETE /experiences/:id(.:format) experiences#destroy
Really feels like a config type of thing
I dont understand why it would be yielding a Sinatra error
Any help appreciated
** Update **
SideKiq and its dependency sinatra is installed in the Gemfile for this rails app.
And I believe that sinatra may be interfering with controller test.
Ok.. so the problem was a collision of mistakes
1.) Because Im using Sidekiq I had the following in my config route
require 'sidekiq/web'
mount Sidekiq::Web => '/sidekiq'
That awesome piece of code mounts a Sinatra based web interface to monitor Sidekiq
2.) If you notice above my directory above is wrong
It was incorrectly spec/controller_spec instead of spec/controller
When that happens rspec doesnt know you are intending to test controllers and doesnt
load all the Get/post helpers , instead in the loaded stack the only thing it
found was sinatra which has a get method..
Solution ..
Move the spec to the proper directory so it properly infers the right dependencies
IT10T error..

rails integration tests pass with ruby -Itest, fail with rake test:integration

I'm following the rails_admin README. I have setup devise and cancan, only users of group admin can access rails_admin.
Here is the test
test "try to access rails_admin as a non admin user" do
get_via_redirect '/users/auth/facebook'
assert_response :success
assert_match 'Successfully authenticated', flash[:notice]
puts User.all.count
puts User.first.name
puts Group.find(User.first.group_id).name
assert_equal Ability.new(User.first).can?(:access, :dashboard), false
puts session
get '/admin'
assert_response :found
assert_redirected_to '/'
assert_match 'You are not authorized', flash[:alert]
end
When I run 'ruby -Itest test/integration/test.rb' all passes. The output is
1
Facebook User
user
{"session_id"=>"5345e64582b2557d0d02cd2011461467", "warden.user.user.key"=>["User", [2], "$2a$04$B.nVokuCXSWOpZ2Ezf60Cu"], "flash"=>#<ActionDispatch::Flash::FlashHash:0xb49d9a4 #used=#<Set: {:notice}>, #closed=false, #flashes={:notice=>"Successfully authenticated from facebook account."}, #now=nil>}
When I run 'bundle exec rake test:integration' the test fails. The output is
1
Facebook User
user
{"session_id"=>"0005d96c17c75d0843166e5dbb4dcc05", "warden.user.user.key"=>["User", [3], "$2a$04$4f5/I9uSZbMWBdCgDA086O"], "flash"=>#<ActionDispatch::Flash::FlashHash:0xa4c0db0 #used=#<Set: {:notice}>, #closed=false, #flashes={:notice=>"Successfully authenticated from facebook account."}, #now=nil>}
F.
Finished tests in 1.054687s, 3.7926 tests/s, 24.6519 assertions/s.
1) Failure:
test_try_to_access_rails_admin_as_a_non_admin_user(RailsAdminTest):
Expected response to be a <:found>, but was <200>
I also tried "assert_select 'body', 'something'". When running rake test:integration it outputs some html like a standard rails_admin dashborad page. Looks like the user is authorized to access rails_admin.
I'm using rails 3.2.6, devise 2.1.2, cancan 1.6.8, rails_admin 0.0.5
Any ideas? Thanks.
I was having the same issues (single test running but not all at once). It seems this is due to rails_admin not being initalized by default when running test.
What helped was setting
SKIP_RAILS_ADMIN_INITIALIZER=false
before calling rake. So now it's
SKIP_RAILS_ADMIN_INITIALIZER=false rake test
for me.
Unfortunately i do not know how to reinitialize rails_admin from inside the tests (there is a rake task to disable but not to enable) and prefixing rake test every time is annoying. Also the tests do run quite a bit longer if rails_admin is initalized (that's propably why it's turned of by default).
We now use a small test helper for those tests which check rails_admin access. The helper returns immediately and logs a warning if SKIP_RAILS_ADMIN_INITIALIZER is true and thus does not run the tests on normal dev systems. On our continuous integration system SKIP_RAILS_ADMIN_INITIALIZER=false is set and the tests are run.

Rails 3 / RSpec: Controller tests -- access to a class function in my test returns an SQLite3::SQLException "no such table" error

I have a Page scaffold that I created through rails generate scaffold. I ran rake db:migrate, and viewing the database through SQLite Database Browser reveals that it in fact exists. I can go to the rails console and run:
Page.all.each {|page| puts page.url}
When I run this, it in fact returns the right results (e.g., the value of url for each record of Page.
What I am trying to do is set up an rspec test in my pages controller test to see that each of the routes I've set up in config/routes.rb actually works. So I have a test defined in spec/controllers/pages_controller_spec.rb as follows:
describe PagesController do
Page.all.each do |page|
describe "GET /#{page.url}" do
it "should be able to get at page at #{page.url}" do
get "/#{page.url}"
response.should be_successful
end
end
end
end
This test does not run. Instead, it generates the following message:
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:202:in `rescue in log': SQLite3::SQLException: no such table: pages: SELECT "pages".* FROM "pages" (ActiveRecord::StatementInvalid)
Does anyone know what is going on? Or, in the alternative, does anyone know why this will work in my Rails Console, but not in my rspec tests?
Thank you so much. I am relatively new to Rails, so still learning a lot!
Run rake db:test:prepare to make sure the database schema is setup in the test environment.

Resources