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?
Related
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
I'm having issues now that i've added devise to my web app, theres a few SO questions on this but they just all point to a readme file and don't actually provide any fixes. I've had to include devise helpers as shown below in order to remove an issue saying the authentication method didn't exist
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
include Devise::TestHelpers
end
If i don't include it i end up with a huge amount of authentication errors:
ActionView::Template::Error: undefined method `authenticate' for nil:NilClass
but if it is included
NoMethodError: undefined method `env' for nil:NilClass
Simply occurs in different places instead, is there an actual fix?
Example
ContactMailerTest#test_should_return_contact_email:
NoMethodError: undefined method `env' for nil:NilClass
Method:
test "should return contact email" do
mail = ContactMailer.contact_email("Email#Email.com", "Example",
"SomeExample", #comment = "Hello")
assert_equal ['info#mynotes.com'], mail.to
assert_equal ['info#mynotes.com'], mail.from
end
This appears to be an issue with "Apparently there are issues with Devise::TestHelpers and integration testing" i am wondering if there is any known fix
Thanks!
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
In some of my controllers I need to use the group(s) that belong to my logged in user(current_user.groups). When I try to test; I don't seem to have this current_user though:
ActionView::Template::Error: undefined method `authenticate' for nil:NilClass
So I figured I should create that current_user with Devise.
I've read the documentation of Devise stating I should add the following to my test_helper.rb:
class ActionController::TestCase
include Devise::TestHelpers
def setup
#request.env["devise.mapping"] = Devise.mappings[:user]
sign_in FactoryGirl.create(:user)
end
end
This doesn't seem to do the trick though; Whenever I run rake test I get the following errors:
1) Error:
ActivitiesControllerTest#test_should_create_activity:
NameError: uninitialized constant ActionController::TestCase::FactoryGirl
test/test_helper.rb:22:in `setup'
You have to include the factory_girl_rails gem in your Gemfile. I usually include it in both the development and test group, but just the test environment is fine for your example.
group :development, :test do
gem 'factory_girl_rails'
end
And then run bundle install.
factory_girl_rails is used when you are creating the user fixture in your test:
sign_in FactoryGirl.create(:user)
Then you need to create a factory (which is almost like a fixture):
rails generate factory_girl:model user
This will create the file: test/factories/users.rb
Read more about factory_girl_rails and how to define factories here: https://github.com/thoughtbot/factory_girl_rails
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.