views:

186

answers:

2

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 "[email protected]"
  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!!

A: 

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
Right..tried that too. no deals.
Trip
@hingara, updated my code above, but i seem to be having trouble with proper syntax..
Trip
+1  A: 

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
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
I updated my scenario above. ;)
Trip
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
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
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
See my edited answer. Rspec does have a learning curve -- your first post with all the comments looked like something I did myself :).
zetetic
YOU ARE AWESOME! Thanks so much Z! :D It worked! On to the next failure lol
Trip