NoamB/sorcery — Issue #125: Example with Rspec and Factory_girl
Author: xlprojects
Created: 2011-10-30 07:15:54+00:00
State: open

Hi,

I was curious if anyone had successfully implemented a test suite with factory_girl... I tried to do so but it seems like my instance don't get assigned...

Since the controller requires the user to be an admin to access the action, I was thinking that I am doing things wrong when creating my user.

A little bit of a background:

**in my spec_helper**
```ruby
RSpec.configure do |config|
...
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
# config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.include Factory::Syntax::Methods
end```

**in my test_helper**
```ruby
class ActiveSupport::TestCase
...
# Add more helper methods to be used by all tests here...
include Sorcery::TestHelpers::Rails
end```

**test/factories/activities.rb**
```ruby
FactoryGirl.define do
salt = "asdasdastr4325234324sdfds"
factory :user do
first_name "Bob"
last_name "Sinclar"
dob "03/12/1982"
genre "M"
sequence(:username) { |n| "bsinclar#{n}" }
email { "#{username}@example.com" }
password "secret"
password_confirmation "secret"
salt salt
crypted_password Sorcery::CryptoProviders::BCrypt.encrypt("secret", salt)
activation_state "active"
admin false

factory :admin_user do
admin true
end
end
end```

**test/factories/users.rb**
```ruby
FactoryGirl.define do
factory :activity do
sequence(:name) { |n| "Activity #{n}" }
category "Activity Category"
end
end```

**in my activities_controller_spec.rb**
```ruby
require 'spec_helper'

describe Admin::ActivitiesController do
let!(:user) { Factory(:admin_user) }
let!(:activity1) { Factory(:activity) }
let!(:activity2) { Factory(:activity) }

describe "#index" do
it "should assign the activities" do
get :index, :format => 'html'
assigns(:activities).to_a.should =~ [activity1, activity2]
response.should render_template("admin/activities/index")
end
end
end```

my admin namespace controller does redirect the non admin users:

**my admin/activities_controller.rb**
```ruby
class Admin::ActivitiesController < Admin::AdminController

def index
@activity = Activity.new
@activities = Activity.order(:category).order(:name).group_by(&:category)
end
...```

Few of my tries: when checking out the assigns hash, I see: "current_user"=>false and also the activities are absent, although if, during debug with pry, I do Activity.count, I see that the two I create with the let statement are there...

So I'm not sure how to debug this at this point... any help is greatly appreciated... (I swear I'll post a full rails 3.1 + rspec + factory_girl example app afterward :P)