3

My users_controller.rb

# GET /users/1/edit
def edit
  @user = current_user
  #@user = User.find(params[:id])
end

my sweet looking users_controller_spec.rb ( notice all my commented out attempts )

describe "Authenticated examples" do
  before(:each) do
    activate_authlogic
    UserSession.create Factory.build(:valid_user)
  end

describe "GET edit" do
  it "assigns the requested user as @user" do
    @user = Factory.create(:valid_user)
    assigns(:user).should be(Factory.build(:valid_user))
  end
end

user.rb - factories

Factory.define :valid_user, :class => User do |u|
  u.username "Trippy"
  u.password "password"
  u.password_confirmation "password"
  u.email "elephant@gmail.com"
  u.single_access_token "k3cFzLIQnZ4MHRmJvJzg"
end

Basically I'm just trying to get this RSpec test to pass in the most appropriate way.

I need to be able to say very simply, that the mock_user is the current_user .

This test passes if I use in my users_controller.rb the @user = User.find(params[:id])

Thanks!!

Trip
  • 26,756
  • 46
  • 158
  • 277

3 Answers3

4

There is a bug in rspec-rails (see https://github.com/rspec/rspec-rails/issues/391) that breaks activate_authlogic when used in RSpec's global before hook.

Instead of

# spec_helper.rb

# In RSpec.configure
config.before :each, :type => :controller do
  activate_authlogic
  user = User.new(:login => "user", :password => "secret")
  UserSession.create(user)
end

you can define a helper

# spec_helper.rb

require 'authlogic/test_case'
module LoginHelper
  include Authlogic::TestCase

  def login_user
    activate_authlogic
    user = User.new(:login => "user", :password => "secret")
    UserSession.create(user)
  end
end

and reference it in a before block when needed

# <controller>_spec.rb

describe "GET 'edit' when logged in" do
  before do
    login_user
  end

  it "should be successful" do
    get 'edit'
    response.should be_success
end
trkoch
  • 2,688
  • 1
  • 16
  • 17
3

I'm not sure if this applies to Rspec 2, but according to the Authlogic docs you need to put this in a before method, or in spec_helper:

include Authlogic::TestCase
activate_authlogic

And then you can create user sessions as you would outside of the test environment.

FWIW I gave up on mocking/stubbing in Authlogic examples, and do @user = Factory.create(:user), who are then logged in with UserSession.create(@user).

EDIT

Here's an attempt using the example you provided. I think the issue you're having is that the object in assigns is not the same as the one you're matching on.

describe "Authenticated examples" do
  before(:each) do
    # assuming you put include Authlogic::TestCase in spec_helper
    activate_authlogic
    @user = Factory.create(:valid_user)
    UserSession.create(@user)
  end

describe "GET edit" do
  it "assigns the requested user as @user" do
   # add a MyModel.stub!(:find) here if the edit action needs it
   get :id => 1 # pass in an ID so the controller doesn't complain
   assigns(:user).should == @user
  end
end
zetetic
  • 47,184
  • 10
  • 111
  • 119
  • I have the `include Authlogic::TestCase` in my spec_helper . The activate_authlogic in my before(:each) like above. I **am** trying to use Factory to make the user. And from one of my examples above, is very close to yours. I'm guessing that right now, my problem comes from a syntactical problem. Ha! Wish me luck ;) – Trip Sep 08 '10 at 13:41
  • Looking at the updated code, did you leave out the `get`? Also, it looks like the result of `Factory.build(:valid_user)` will be different after each call, unless you've got some sort of singleton pattern at work in the factory. – zetetic Sep 08 '10 at 15:50
  • Thanks Zetetic for responding. I honestly havn't the slightest clue what I'm doing here. o much so that even if it did pass, it might not actually be testing anything. :D . That said, is there anything you could say explicitly to allow it to pass? It's so far, the most common set up for authlogic and rspec based off of this site http://rails.anyware-technologies.com.br/2009/04/21/tdd-on-rails-4-rspec-authlogic-factory_girl-and-resource_controller/ . – Trip Sep 08 '10 at 16:28
  • I included the factory_girl model above. Been stuck on this one code refusing to workaround it for three days now. :D I suppose rspec, like rails, has a learning curve, that once you get past the small battles, everything becomes clear. Thanks Zetetic. – Trip Sep 08 '10 at 17:30
  • See my edited answer. Rspec does have a learning curve -- your first post with all the comments looked like something I did myself :). – zetetic Sep 08 '10 at 17:43
  • YOU ARE AWESOME! Thanks so much Z! :D It worked! On to the next failure lol – Trip Sep 08 '10 at 18:24
1

All are on this page :

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

You need put some information :

 require "authlogic/test_case" # include at the top of test_helper.rb
  setup :activate_authlogic # run before tests are executed
  UserSession.create(users(:whomever)) # logs a user in

Inf this case you just need change your users(:whomever) by your mock_user

shingara
  • 46,608
  • 11
  • 99
  • 105
  • @hingara, updated my code above, but i seem to be having trouble with proper syntax.. – Trip Sep 08 '10 at 17:31