0

I have following code

@user = FactoryGirl.create(:admin)
post :create, company: attributes_for(:company)
expect Company.count.should == 1

I can't execute post because i have authenticate user method on all controllers, how can i sign in user before that action , i am using devise.

2 Answers 2

3

You can stub current_user. Having created @user, you could do this:

before(:each) do
  allow_any_instance_of(ApplicationController).
    to receive(:current_user).and_return(@user)
end

Hope this works?

Sign up to request clarification or add additional context in comments.

Comments

2

You can add the following into spec/rails_helper.rb:

require 'devise'

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

and using sign_in method before test like sign_in @user

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.