metamaps--metamaps/spec/support/controller_helpers.rb
Devin Howard 7d4da81272 Update code style automatically using rubocop gem (#563)
* install rubocop

* 1961 automatic rubocop fixes

* update rubocop.yml to ignore half of the remaining cops

* rubocop lint warnings

* random other warnings fixed
2016-07-26 08:14:23 +08:00

26 lines
798 B
Ruby

# https://github.com/plataformatec/devise/wiki/How-To:-Stub-authentication-in-controller-specs
require 'devise'
module ControllerHelpers
# rubocop:disable Metrics/AbcSize
def sign_in(user = create(:user))
if user.nil? # simulate unauthenticated
allow(request.env['warden']).to(
receive(:authenticate!).and_throw(:warden, scope: :user)
)
else # simulate authenticated
allow_message_expectations_on_nil
allow(request.env['warden']).to(
receive(:authenticate!).and_return(user)
)
end
allow(controller).to receive(:current_user).and_return(user)
end
# rubocop:enable Metrics/AbcSize
end
RSpec.configure do |config|
config.include Devise::TestHelpers, type: :controller
config.include ControllerHelpers, type: :controller
end