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
Related
Rails 5.1.6
Ruby 2.5.0
I am trying to run a simple test for a redirect in one of my controllers using Shoulda Matcher gem (following the documentation) and minitest:
home_controller.rb:
class HomeController < ApplicationController
def index
#redirect on login
if user_signed_in?
redirect_to controller: 'home', action: "dashboard_#{current_user.user_role}"
end
end
test/controllers/home_controller_test.rb:
class HomeControllerTest < ActionController::TestCase
context 'GET #index' do
setup { get :index }
should redirect_to(action: "dashboard_#{current_user.user_role}")
end
end
Error:
Undefined method current_user for homecontrollertest
I'm using Devise and was wondering if anyone could assist to get my test to work? I can provide more info if required.
EDIT:
Tried this:
home_controller_test.rb
require 'test_helper'
class HomeControllerTest < ActionController::TestCase
include Devise::Test::ControllerHelpers
context 'GET #index' do
user = users(:one)
sign_in user
get :index
should redirect_to(action: "dashboard_#{user.user_role}")
end
end
users.yml
one:
name: 'John'
email: 'some#user.com'
encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>
user_role: 1
Gemfile
gem 'shoulda', '~> 3.5'
gem 'shoulda-matchers', '~> 2.0'
Test_helper.rb
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
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...
end
Get undefined method users error.
NoMethodError: undefined method `users' for HomeControllerTest:Class
/mnt/c/code/studytaps/test/controllers/home_controller_test.rb:9:in `block in <class:HomeControllerTest>'
/mnt/c/code/studytaps/test/controllers/home_controller_test.rb:8:in `<class:HomeControllerTest>'
/mnt/c/code/studytaps/test/controllers/home_controller_test.rb:4:in `<top (required)>'
Tasks: TOP => test
(See full trace by running task with --trace)
You need to include devise test helper to your test
class HomeControllerTest < ActionController::TestCase
include Devise::Test::ControllerHelpers
context 'GET #index' do
user = users(:one) # if you use fixtures
user = create :user # if you use FactoryBot
sign_in user
get :index
should redirect_to(action: "dashboard_#{user.user_role}")
end
end
I am new to Rails. I want to test my activeadmin resources as mentioned in the following link
https://github.com/activeadmin/activeadmin/wiki/Testing-your-ActiveAdmin-controllers-with-RSpec
My simple test as follows
require 'spec_helper'
describe "activeadmin resources" do
it "should have admin user resource" do
ActiveAdmin.application.namespaces[:admin].resources.should have_key("AdminUser")
end
end
But I am getting the error
NameError: uninitialized constant ActiveAdmin`
I tried to require activeadmin gem in spec_helper, but no use. Any idea?
Just by changing
require 'spec_helper'
to
require 'rails_helper'
the error was resolved.
I recently upgraded the RSpec version of my Rails 4 app. This is my setup:
# /Gemfile:
source 'https://rubygems.org'
group :development, :test do
gem 'rspec-rails', '~> 2.99'
...
end
# /spec/controllers/companies_controller_spec.rb:
require 'spec_helper'
describe CompaniesController, :type => :controller do
before(:all) do
#user = FactoryGirl.create(:user)
sign_in(#user)
end
describe 'GET #new' do
it "renders the :new view" do
get :new
expect(response).to render_template :new
end
end
...
end
# /spec/support/utilities.rb:
def sign_in(user)
cookies[:remember_token] = user.remember_token
end
The problem is that most of my tests are now failing and I've been trying to fix it for hours, but to no avail.
This is the error I get for nearly all tests that require a user sign in:
Failure/Error: sign_in(#user)
NoMethodError:
undefined method `cookie_jar' for nil:NilClass
# ./spec/support/utilities.rb:2:in `sign_in'
The #user object gets created by FactoryGirl, however. I've checked that in the test database and in the logs.
Why is my method for signing in users not working?
It worked like a charm before upgrading to a later version of RSpec.
Thanks for any help.
In a before(:all) you shouldn't be doing stuff that only makes sense in the context of a single example.
In particular your sign_in method fiddles with the cookies, which is implicitly the current request/controller's cookies. This doesn't make sense in a before(:all) since each example is supposed to have a separate request - the exception occurs because there is no current request. Change the before(:all) to a before(:each) and you should be ok.
Rspec used to be more lenient in this respect, but 2.99 tightened this up, deprecation warnings were also added for similar uses in some of the earlier 2.x versions (if you had depreciations about using let/subject in a before(:all) block, that was it)
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?