Search code examples
rubyruby-on-rails-4rspecrspec-rails

How to sign in user when testing controllers with rspec


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.


Solution

  • 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