Testing Rails application with ActsAsAuthenticated plugin

Acts As Authenticated plugin is first choice when You want add authentication to Your Rails application. Next step is to add some test to Your code (everybody writes tests, right?). But looks like Google have no clue for newbies how to setup.

So here comes simple recipe: when You add ActAsAuthenticated plugin to Your app, You already have some test fixtures (test/fixtures/users.yml), so You need to load it in tests, and access action in controller with proper credentials.

First add fixtures :users in test class (watch exactly when You type this line, since it is not first line of Your auto generated test class). Next write test like:

  def test_list
    get :list, {}, {:user => users(:aaron).id }

    assert_response :success
    assert_template 'list'
    assert_not_nil assigns(:routes)
  end

Interesting thing is second hash passed to get method. It contains session variables. AAA uses variable user in session to pass ID of logged user. So we are setting it to ID of user aaaron from our test fixtures.

It is quite simple, but I had to check AAA internals to find variable in session responsible for passing login user info. So maybe someone will find it useful when first uses Google ;-)) like I did.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.