Sha256: d774c0ee247b27becaa51a2e5d1ae873f36e48f445fc8c2c61249ffa48ebfd14

Contents?: true

Size: 1.08 KB

Versions: 8

Compression:

Stored size: 1.08 KB

Contents

module RequestSpecHelpers
  # https://github.com/plataformatec/devise/wiki/How-To:-sign-in-and-out-a-user-in-Request-type-specs-(specs-tagged-with-type:-:request)
  include Warden::Test::Helpers

  # def self.included(base)
  #   base.before(:each) { Warden.test_mode! }
  #   base.after(:each) { Warden.test_reset! }
  # end

  # def sign_in(resource)
  #   login_as(resource, scope: warden_scope(resource))
  # end

  # def sign_out(resource)
  #   logout(warden_scope(resource))
  # end

  # https://makandracards.com/makandra/37161-rspec-devise-how-to-sign-in-users-in-request-specs
  def sign_in(resource_or_scope, resource = nil)
    resource ||= resource_or_scope
    scope = Devise::Mapping.find_scope!(resource_or_scope)
    login_as(resource, scope: scope)
  end

  def sign_out(resource_or_scope)
    scope = Devise::Mapping.find_scope!(resource_or_scope)
    logout(scope)
  end

  # http://matthewlehner.net/rails-api-testing-guidelines/
  def response_body_as_json
    JSON.parse(response.body)
  end

  private

  def warden_scope(resource)
    resource.class.name.underscore.to_sym
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
pwb-1.4.0 spec/support/request_spec_helpers.rb
pwb-1.3.0 spec/support/request_spec_helpers.rb
pwb-1.2.0 spec/support/request_spec_helpers.rb
pwb-1.1.1 spec/support/request_spec_helpers.rb
pwb-1.0.0 spec/support/request_spec_helpers.rb
pwb-0.1.1 spec/support/request_spec_helpers.rb
pwb-0.1.0 spec/support/request_spec_helpers.rb
pwb-0.0.2 spec/support/request_spec_helpers.rb