Integration Testing Authlogic before_filter :require_admin_user Problem
I'm not able to access urls in my integration test that require an admin user despite creating an admin user session. My test fails on a 302 error.
class NewsItemsController < ApplicationController
before_filter :require_admin_user, :except => [:show, :index, :feed]
etc...
end
--test/inetgration/admin_s开发者_StackOverflow中文版tories.rb --
require 'test_helper'
class AdminStoriesTest < ActionController::IntegrationTest
fixtures :all
setup :activate_authlogic
# if user is an admin he can create a new news_item
def test_creating_a_news_item
assert UserSession.create(users(:admin))
get "news_items/new"
assert_response :success
#etc...
end
end
I've got the following in the test.log:
Unable to load roles_user, underlying cause no such file to load -- roles_user
My fixtures file is named roles_users.yml as you would expect - so unsure of how to resolve this...
I think the new action is redirecting to the login screen.
This happens because you are not logged in. Try:
get "news_items/new", {}, { 'user_id' => 0 }
or what the user id actually is.
The first hash is the request hash, the second is the session hash.
You may also need to put include Authlogic::TestCase in your class. From my app (rspec, but same idea)
describe InvitationsController do
include Authlogic::TestCase
setup :activate_authlogic
describe 'sending invitations' do
.........
Does your application require users to be activated? If so, is this user?
If that doesn't help. perhaps paste in some debug output, maybe an inspect of the created User.
I was also having a problem with Authlogic in Integration tests, and the following fix (from this post) was a successful workaround for me.
Replace:
assert UserSession.create(users(:admin))
With:
post 'user_session', :user_session => {:email => 'someone@example.com', :password => 'password'}
精彩评论