module Clearance::Testing::ControllerHelpers

Provides helpers to your controller specs. These are typically used in tests by requiring `clearance/rspec` or `clearance/test_unit` as appropriate in your `rails_helper.rb` or `test_helper.rb` files.

Public Instance Methods

factory_module(provider) click to toggle source

Determines the appropriate factory library

@api private @raise [RuntimeError] if both FactoryGirl and FactoryBot are not

defined.
# File lib/clearance/testing/controller_helpers.rb, line 46
def factory_module(provider)
  if defined?(FactoryBot)
    FactoryBot
  elsif defined?(FactoryGirl)
    FactoryGirl
  else
    raise("Clearance's `#{provider}` helper requires factory_bot")
  end
end
setup_controller_request_and_response() click to toggle source

@api private

Calls superclass method
# File lib/clearance/testing/controller_helpers.rb, line 9
def setup_controller_request_and_response
  super
  @request.env[:clearance] = Clearance::Session.new(@request.env)
end
sign_in() click to toggle source

Signs in a user that is created using FactoryGirl. The factory name is derrived from your `user_class` Clearance configuration.

@raise [RuntimeError] if FactoryGirl is not defined.

# File lib/clearance/testing/controller_helpers.rb, line 19
def sign_in
  constructor = factory_module("sign_in")

  factory = Clearance.configuration.user_model.to_s.underscore.to_sym
  sign_in_as constructor.create(factory)
end
sign_in_as(user) click to toggle source

Signs in the provided user.

@return user

# File lib/clearance/testing/controller_helpers.rb, line 29
def sign_in_as(user)
  @request.env[:clearance].sign_in(user)
  user
end
sign_out() click to toggle source

Signs out a user that may be signed in.

@return [void]

# File lib/clearance/testing/controller_helpers.rb, line 37
def sign_out
  @request.env[:clearance].sign_out
end