5

In a few of my controllers I have a before_filter that checks if a user is logged in? for CRUD actions.

application.rb

def logged_in?
  unless current_user
    redirect_to root_path
  end
end

private
def current_user_session
  return @current_user_session if defined?(@current_user_session)
  @current_user_session = UserSession.find
end

def current_user
  return @current_user if defined?(@current_user)
  @current_user = current_user_session && current_user_session.record
end

But now my functional tests fail because its redirecting to root. So I need a way to simulate that a session has been created but nothing I've tried has worked. Heres what I have right now and the tests pretty much ignore it:

test_helper.rb

class ActionController::TestCase
  setup :activate_authlogic
end

posts_controller_test.rb

class PostsControllerTest < ActionController::TestCase
  setup do
    UserSession.create(:username => "dmix", :password => "12345")
  end

  test "should get new" do  
    get :new
    assert_response :success
  end

Am I missing something?

5 Answers 5

5

You should pass ActiveRecord object in UserSession.create

Something like:

u = users(:dmix)
UserSession.create(u)
Sign up to request clarification or add additional context in comments.

2 Comments

I really encourage you to move away from fixtures for testing if you have an app that doesn't already depend on them. They are tough to maintain, and just frustrating really. Check out the railscast, Factories not Fixtures.
By creating a user like that you're not testing that the appropriate checks are called in your controller (e.g. must be logged in, must be an admin, etc). It's better to mock the expected methods to ensure they get called, e.g. for Mocha: mock(@controller).expects(:current_user).returns(@user)
4

http://rdoc.info/github/binarylogic/authlogic/master/Authlogic/TestCase

First you need to activate AuthLogic so that you can use it in your tests.

setup :activate_authlogic

Then you need a valid user record as Anton Mironov pointed out.

2 Comments

That link to the docs is dead, try here: rdoc.info/github/binarylogic/authlogic/master/Authlogic/…
That page is kind of badly written; it's not clear where the setup line is supposed to go.
3

All I do in my rspec tests for my controller is create a User with Machinist and then assign that user to be the current_user.

def login_user(options = {})
  user = User.make(options)
  @controller.stub!(:current_user).and_return(user)
end

and this attaches the current_user to the controller, which would mean that your logged_in? method would work in your tests.

You obviously would probably need to adapt this to work in Test::Unit, and without Machinist if you don't use it, as I use rspec, but I'm sure the principle is the same.

Comments

1

Put this in test_helper.rb if you want all your tests to setup Authlogic:

class ActionController::TestCase
  def self.inherited(subclass)
    subclass.instance_eval do
      setup :activate_authlogic
    end
  end
end

1 Comment

This bit broke my tests in ruby 1.9 and was pretty hard to track down. I'd just do setup :activate_authlogic at the class level rather than inside the instance_eval.
0

Here is a link to the AuthLogic test documentation. It's an important one but is a bit buried (the same link Simone posted, however his didn't work anymore).

That page has all the information you need to get going testing you application using AuthLogic for authentication.

Additionally, as railsninja suggested, use factories not fixtures. Take a look at factory_girl and machinist; pick your poison, they are both good.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.