I'm trying to get started with controller tests, and I'm not sure what I'm doing wrong. Here's my code, and the error it's producing:
require 'test_helper'
class InvProcure::UserImportsControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::ControllerHelpers
test "should get index" do
user = users(:foobars_admin)
sign_in(:user, user)
get inv_procure_user_imports_path
assert_response :success
end
end
NoMethodError: undefined method `env' for nil:NilClass
/home/blaine/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/devise-4.7.1/lib/devise/test/controller_helpers.rb:42:in `setup_controller_for_warden'
It looks like the error might be happening when including devise test helpers, or when calling sign_in.
From the doc: https://www.rubydoc.info/gems/devise/Devise/Test/ControllerHelpers
Devise::Test::ControllerHelpers provides a facility to test controllers in isolation when using ActionController::TestCase allowing you to quickly sign_in or sign_out a user. Do not use Devise::Test::ControllerHelpers in integration tests.
You are inheriting the integration test class instead of the controller test class which is the default for controller tests now (From Rails 5 the controller tests are generated with the parent class as ActionDispatch::IntegrationTest instead of ActionController::TestCase). Devise::Test::ControllerHelpers was built for ActionController::TestCase and not for integration tests.
You can instead try to use Devise::Test::IntegrationHelpers which should have similar methods for integration tests.
Document: https://www.rubydoc.info/gems/devise/Devise/Test/IntegrationHelpers
Related
I'm trying to run a rake test using devise (using a user that's been created by database seeds)
Running rake test produces the following error:
Failure:
CustomersControllerTest#test_should_get_index [/home/ubuntu/workspace/test/controllers/customers_controller_test.rb:7]:
Expected response to be a <2XX: success>, but was a <302: Found> redirect to <http://www.example.com/users/sign_in>
Response body: <html><body>You are being redirected.</body></html>
My test looks like this:
test "should get index" do
get customers_url
assert_response :success
end
It's a very simple test, but I don't know how to actually login to the site before performing the test.
To test actions that require an authenticated user you can use Devise's sign_in and sign_out helpers. These come from Devise::Test::ControllerHelpers on your test case or its parent class (or Devise::Test::IntegrationHelpers for Rails 5+):
class CustomersControllerTest < ActionController::TestCase
include Devise::Test::ControllerHelpers # <-- Include helpers
test "should get index" do
sign_in User.create(...) # <-- Create and authenticate a user
get customers_url
assert_response :success
end
end
See the controller tests section in the Devise README for details.
Alright, a little embarrassed I asked a very similar question yesterday, but we're stuck again.
We've fixed all of our controller tests, and started writing integration tests. We're encountering errors on all of our integration tests, even the famous assert = true:
site_layout_test.rb
require 'test_helper'
class SiteLayoutTest < ActionDispatch::IntegrationTest
test "the truth" do
assert true
end
#commenting out our real tests for debugging
=begin
test "top navigation bar links" do
get 'beta/index'
assert_template 'beta/index'
assert_select "a[href=?]", home_path
assert_select "a[href=?]", how_it_works_path
to do add map, community, sign up, login paths
to do: add paths to links on dropdown if user is logged in
end
=end
end
Terminal Test Results
12:31:32 - INFO - Running: test/integration/site_layout_test.rb
Started with run options --seed 27747
ERROR["test_the_truth", SiteLayoutTest, 2015-10-25 11:36:28 +0800]
test_the_truth#SiteLayoutTest (1445744188.33s)
NoMethodError: NoMethodError: undefined method `env' for nil:NilClass
1/1: [===================================] 100% Time: 00:00:00, Time: 00:00:00
Finished in 0.05380s
1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
include Devise::TestHelpers
# Add more helper methods to be used by all tests here...
#
def setup_variables
#base_title = "TripHappy"
end
end
Unfortunately, that error message gives us very little clue as to where the error is occurring. I tried reading up on Minitests, but without an idea of where to look I'm fairly lost.
Thank you in advanced!
For reference, we're following M. Harti's Rails Tutorial, which means we're using Guard and Minitest Reporters. We also have a login system via Devise, if that affects anything.
Solution:
It was an issue with the Devise. I included include Devise::TestHelpers in class ActiveSupport::TestCase instead of class ActionController::TestCase.
It was an issue with the Devise. I included include Devise::TestHelpers in class ActiveSupport::TestCase instead of class ActionController::TestCase
I am trying to move a helper method from a controller test to the test_helper.rb:
# example_controller_test.rb
require 'test_helper'
class ExampleControllerTest < ActionController::TestCase
should 'get index' do
turn_off_authorization
get :show
assert_response :success
end
end
# test_helper.rb
class ActionController::TestCase
def turn_off_authorization
ApplicationController.any_instance
.expects(:authorize_action!)
.returns(true)
end
end
However, I'm getting an error:
NameError: undefined local variable or method `turn_off_authorization' for #<ExampleControllerTest:0x000000067d6080>
What am I doing wrong?
Turns out that I had to wrap the helper method into a module:
# test_helper.rb
class ActionController::TestCase
module CheatWithAuth do
def turn_off_authorization
# some code goes here
end
end
include CheatWithAuth
end
I still don't know why the original version didn't work.
The idea came from another answer:
How do I write a helper for an integration test in Rails?
Edit: Another solution just came from my friend:
# test_helper.rb
class ActiveSupport::TestCase
def turn_off_authorization
# some code goes here
end
end
Note that ActiveSupport::TestCase (not ActionController::TestCase) is being used here.
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.
I'm having some difficulties in testing devise with shoulda:
2) Error:
test: handle :index logged as admin should redirect to Daily page.
(Admin::DailyClosesControllerTest):
NoMethodError: undefined method `env' for nil:NilClass
devise (1.0.6) [v] lib/devise/test_helpers.rb:52:in
`setup_controller_for_warden'
I have this in my test_helper:
include Devise::TestHelpers
Thoughts ?
Thanks in advance,
Cristi
include Devise::TestHelpers doesn't go in the test_helper.rb file, but rather inside the scope of the individual testing classes. Just like their README shows:
class ActionController::TestCase
include Devise::TestHelpers
end
I'm not sure if rspeicher is fully correct, but putting:
class ActionController::TestCase
include Devise::TestHelpers
end
at the very bottom of test_helper.rb (yes after the END of the class ActiveSupport::TestCase) should work. It has for 3 or 4 projects of mine so far, including one i'm working on today.
You then can use sign_in users(:one) if you are using fixtures, in your tests. Unless shoulda is messing it up?