Reputation: 85
i'm trying to write some controller tests which requires authentication, i'm currently using authlogic 6.4.3 and rails 7
The problem is that it always throws this error:
Authlogic::Session::Activation::NotActivatedError: You must activate the Authlogic::Session::Base.controller with a controller object before creating objects
If I added it as setup:activate_authlogic it gave me this error: NoMethodError: undefined method 'activate_authlogic' for #<VideosTest:0x00007f0fb982f0d0>
but I changed it to setup do and now the error is Authlogic::Session::Activation::NotActivatedError:
this is my test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "authlogic/test_case"
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all
# Add more helper methods to be used by all tests here...
end
# Define active_authlogic into all setup
class ActionController::TestCase
setup do
:activate_authlogic
end
def login(user)
post sesiones_url, :params => { :email => user.email, :password => 'password' }
end
end
this is my test:
require 'test_helper'
class VideosTest < ActionController::TestCase
setup do
@video = video(:one)
end
test "should get new" do
Sesion.create(usuarios(:admin))
get :new
assert_response :success
end
end
This also happens with similar tests that requires authentication.
I also tried to add a helper method for the login but it throws this error:
ActionController::UrlGenerationError: No route matches {:action=>"http://test.host/sesiones", :controller=>"videos", :email=>"a@a.com", :password=>"password"}
I'm running out of ideas
What is the correct way to write tests with authlogic? Or is there a way to bypass authentication in tests?
Upvotes: 0
Views: 97
Reputation: 6899
Your test should inherit from ActionDispatch::IntegrationTest
instead of ActionController::TestCase
.
Rails discourages the use of functional tests in favor of integration tests (use
ActionDispatch::IntegrationTest
).New Rails applications no longer generate functional style controller tests and they should only be used for backward compatibility. Integration style controller tests perform actual requests, whereas functional style controller tests merely simulate a request. Besides, integration tests are as fast as functional tests and provide lot of helpers such as
as
,parsed_body
for effective testing of controller actions including even API endpoints.https://api.rubyonrails.org/v7.0.0/classes/ActionController/TestCase.html
Upvotes: 0