How to drown user_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 (note all my attempts to comment)

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 pass this RSpec test in the most appropriate way.

I need to say very simply what mock_userit is current_user.

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

Thank!!

+3
source share
3 answers

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

include Authlogic::TestCase
activate_authlogic

, .

FWIW /stubbing Authlogic @user = Factory.create(:user), UserSession.create(@user).

. , , , , assigns , .

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
+3

rspec-rails (. https://github.com/rspec/rspec-rails/issues/391), activ_authlogic, RSpec global hook.

# 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

# 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

# <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
+4

:

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

:

 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

users(:whomever) mock_user

+1

Source: https://habr.com/ru/post/1763632/


All Articles