开发者

How to stub an users_controller | authlogic, rspec2, rails3, factory_girl

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开发者_JAVA技巧 - 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!!


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜