Authlogic and functional tests - Authlogic::Session::Activation::NotActivatedError: You must activate - ruby-on-rails

Im getting the errors below despite following the documentation.
In test_helper.rb
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require "authlogic/test_case"
require 'test_help'
require 'shoulda'
require File.dirname(__FILE__) + "/factories"
In my functional test
require 'test_helper'
class SentencesControllerTest < ActionController::TestCase
setup do
:activate_authlogic
end
context "logged in" do
setup do
#user = Factory(:user)
UserSession.create(#user.id)
end
context "on GET to :new" do
setup do
get :new
end
should "present form with text field" do
assert_select('form#new_sentence') do
assert_select('textarea#sentence_text')
end
end
end
end #context logged in.
end
in environments.rb
config.gem "authlogic"
Im not sure why it isnt working. Can anyone help out on this?
Authlogic::Session::Activation::NotActivatedError: You must activate the Authlogic::Session::Base.controller with a controller object before creating objects
authlogic (2.1.3) lib/authlogic/session/activation.rb:47:in `initialize'
authlogic (2.1.3) lib/authlogic/session/klass.rb:64:in `initialize'
authlogic (2.1.3) lib/authlogic/session/scopes.rb:79:in `initialize'
authlogic (2.1.3) lib/authlogic/session/existence.rb:29:in `new'
authlogic (2.1.3) lib/authlogic/session/existence.rb:29:in `create'
test/functional/sentences_controller_test.rb:11:in `__bind_1270172858_922804'
shoulda (2.10.3) lib/shoulda/context.rb:380:in `call'
shoulda (2.10.3) lib/shoulda/context.rb:380:in `run_current_setup_blocks'
shoulda (2.10.3) lib/shoulda/context.rb:379:in `each'
shoulda (2.10.3) lib/shoulda/context.rb:379:in `run_current_setup_blocks'
shoulda (2.10.3) lib/shoulda/context.rb:371:in `run_all_setup_blocks'
shoulda (2.10.3) lib/shoulda/context.rb:375:in `run_parent_setup_blocks'
shoulda (2.10.3) lib/shoulda/context.rb:359:in `test: logged in on GET to :new should present form with text field. '
/opt/rubymine/rb/testing/patch/testunit/test/unit/ui/testrunnermediator.rb:36:in `run_suite'
/opt/rubymine/rb/testing/patch/testunit/test/unit/ui/teamcity/testrunner.rb:215:in `start_mediator'
/opt/rubymine/rb/testing/patch/testunit/test/unit/ui/teamcity/testrunner.rb:191:in `start'

Should:
class SentencesControllerTest < ActionController::TestCase
setup do
:activate_authlogic
end
...
be:
class SentencesControllerTest < ActionController::TestCase
def setup # setup should be its own method, prefixed with "def"
activate_authlogic # note the lack of a ":"
end
...
If, alternatively, you're following the Rails testing tutorial, it may have a single-line setup deal like:
setup :activate_authlogic # note the USE of a ":" here - not sure why it's different between this and when you put it in its own method but that might be the answer for you

Related

Rails: Shoulda matcher, Minitest and Devise query

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

undefined method `authenticate' for nil:NilClass While Testing Custom OmniAuth Strategy in Rails With Devise

I wrote a custom OmniAuth strategy and am now trying to write Rspec tests around it, but I keep getting this error:
NoMethodError:
undefined method `authenticate' for nil:NilClass
# ./controllers/application_controller.rb:58:in `block in <class:ApplicationController>'
# ./lib/omniauth/custom_strategy.rb:135:in `block in callback_phase'
# ./lib/omniauth/custom_strategy.rb:119:in `callback_phase'
# ./spec/lib/omniauth/custom_strategy_spec.rb:62:in `block (3 levels) in <top (required)>'
After digging through the Devise code, I found that the error was originating in lib/devise/controllers/helpers.rb, where the current_user method gets defined in ApplicationController:
def current_#{mapping}
#current_#{mapping} ||= warden.authenticate(:scope => :#{mapping})
end
In this case, warden is defined as:
def warden
request.env['warden']
end
So, in my spec, since it's not a controller spec, I put in custom code to create a Rack app (as seen here: https://github.com/mkdynamic/omniauth/blob/master/spec/omniauth/strategies/developer_spec.rb) like so:
# Outside of the spec block:
RSpec.configure do |config|
config.include Rack::Test::Methods
end
# Inside the spec block:
let(:app){ Rack::Builder.new do |b|
b.use Rack::Session::Cookie
b.use OmniAuth::Strategies::CustomStrategy
b.run lambda{|env| [200, {}, ['Not Found']]}
end.to_app }
The problem here is that the Rack::Test::Methods don't include a request object, like Rspec provides in its controller tests. So, request.env is not defined and therefore warden is not defined.
I tried including Devise::TestHelpers, as described here (All Ruby tests raising: undefined method `authenticate' for nil:NilClass), but since #request is not defined, it throws an error.
I've also tried many other solutions, but none seem to deal with non-controller tests, since they all rely on a request object. Has anyone had experience with these issues and can shed some light on possible solutions?
Thanks in advance.

NoMethodError attempting to access fixture

I am working on migrating the test suite for my project to rspec. I am getting the following error trying to access my fixtures:
undefined method `[]' for nil:NilClass
Here is an example spec file:
require 'rails_helper'
feature "edit contacts" do
fixtures :contacts
before(:all) do
#thing = contacts(:two)
end
scenario "do a thing" do
visit new_contact_path
end
end
I get the error in the before block, on the call to contacts(:two)
Here is config info from rails_helper.rb
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
config.fixture_path = "#{::Rails.root}/spec/fixtures"
end
Here is version info from Gemfile.lock:
rails (4.0.13)
rspec-core (3.2.2)
rspec-expectations (3.2.0)
rspec-mocks (3.2.1)
rspec-rails (3.2.1)
rspec-support (3.2.2)
I'm happy to provide any additional supporting information if needed. Thanks for taking a look, any help appreciated!
ok - finally resolved this with the help of a coworker. The issue was that I should have been using before(:each) instead of before(:all).
Apparently, using :each triggers a 'before_setup' callback in ActiveRecord::FixtureSet which sets up all the fixtures, and :all doesn't.
According to the docs:
before(:each) blocks are run before each example before(:all) blocks are run once before all of the examples in a group

rspec-rails and factory girl block in <top (required)>': undefined method `build'

I have a new Rails 4 project with FactoryGirl and rSpec. In my spec_helper.rb I have:
# lots of stuff
RSpec.configure do |config|
# more stuff
config.include FactoryGirl::Syntax::Methods
end
I also removed the rspec/autorun require in this file.
A simple spec:
require 'spec_helper'
describe User do
build(:user)
end
with a simple factory:
FactoryGirl.define do
factory :user do
email "somename#someplace.com"
end
end
Fails with the following message.
`block in <top (required)>': undefined method `build' for #<Class:0x007fd46d0e3848> (NoMethodError)
However, if I explicitly qualify build in the spec like this it passes:
require 'spec_helper'
describe User do
FactoryGirl.build(:user)
end
What can I do so I don't have to add FactoryGirl every time?
The methods passed to config.include are only included by RSpec within the it, let, before and after blocks, not at the top level of describe. Since that's where you generally need to put your setup and test logic, in practice it's not really an issue.

Include helper not works

I'm trying to include some helpers to my test but I can't make that it works.
I got the following error:
/home/edu/.rvm/rubies/ruby-1.9.3-p392/bin/ruby -S rspec ./spec/features/customers_spec.rb ./spec/features/login_spec.rb ./spec/features/products_spec.rb ./spec/features/suppliers_spec.rb
/home/edu/Desktop/rails_proyects/gg/spec/support/features.rb:2:in `block in <top (required)>': uninitialized constant MyHelp (NameError)
from /home/edu/.rvm/gems/ruby-1.9.3-p392#gg/gems/rspec-core-2.14.6/lib/rspec/core.rb:120:in `configure'
from /home/edu/Desktop/rails_proyects/gg/spec/support/features.rb:1:in `<top (required)>'
I have this:
# spec/support/features/session_helper.rb
module MyHelp
module SessionHelpers
...
def sign_in
...
end
end
end
# spec/support/features.rb
RSpec.configure do |config|
config.include MyHelp::SessionHelpers, type: :feature
end
I'm using it here:
# spec/features/login_spec.rb
require 'spec_helper'
feature "Login" do
scenario "with valid credentials" do
user = create(:user)
sign_in user.email, user.password
page.should have_content(I18n.t('layouts.header.exit', locale: 'es'))
end
end
I'm using:
rspec (2.14.1)
rspec-core (2.14.6, 2.14.5)
rspec-expectations (2.14.3, 2.14.2)
rspec-mocks (2.14.4, 2.14.3)
rspec-rails (2.14.0)
ruby 1.9.3p392
rails 3.2.13
Can someone help me with this?
thank you.
It looks like you just need to require the new helper before you try to use it in spec/support/features.rb
require Rails.root.join('spec/support/features/session_helper')
Also, it's best practice to have your class/module match the file name, so either the file should be pluralized, or the helper singularized.

Resources