views:

565

answers:

3

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_stories.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...

A: 

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.

Thijs Wouters
get "news_items/new", {}, { 'user_id' => '1' }does not help - still get the 302 error
Carmen
Indeed, this is for restful_authentication
Thijs Wouters
A: 

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.

Mike H
I've added test.log error as an edit
Carmen
A: 

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 => '[email protected]', :password => 'password'}
Jeff Terrell