Nastiness of restful_authentication RSpecs

I use the plugin restful_authentication to handle logged in sessions. It makes modules to include in your controller and also makes specs if you have spec directory. All fine, but I cannot live with one login session for the websites; I need “admin” and “client” area. Each session will be verified against separate models, i.e. Client and User models in this case. The problem was with the spec for these tests that were generated by the plugin.

The generator also creates authenticated test helpers, which in my case lie in their own namespace. so I have:

/lib/client/authenticated_test_helper.rb
/lib/admin/authenticated_test_helper.rb

But the typical spec generated looks like this:

require File.dirname(__FILE__) + '/../spec_helper'

# Be sure to include AuthenticatedTestHelper in spec/spec_helper.rb instead.
# Then, you can remove it from this and the functional test.
include AuthenticatedTestHelper

describe User do
...

In my case I had to modify it to include from Admin::AuthenticatedTestHelper or Client::AuthenticatedTestHelper as it fits the model.

For no obvious reason, the tests fail. I finally found the problem with the generated spec that included the module

include Client::AuthenticatedTestHelper
describe User do
...

It includes the AuthenticatedTestHelper outside the describe block. This gets into trouble with another model spec when it is also called AuthenticatedTestHelper but different namespace.

Anyway, the fix is to include with in the describe block, as in:

describe User do
  include Client::AuthenticatedTestHelper
...

Leave a Comment