2

I'm writing RSpec tests for my SessionsController. My tests work fine when testing session#create with valid credentials. However, I want to also write tests for what happens when the users credentials are invalid, such as redirecting back to the sign in page, setting a flash alert, etc. But for any of these tests, I'm getting an error:

1) SessionsController POST #create when password is INCORRECT
  Failure/Error: post :create, user: {username: 'Example', password: ''}
  ArgumentError:
    uncaught throw :warden
  # ./spec/support/default_params.rb:7:in `process_with_default_params'
  # ./spec/controllers/sessions_controller_spec.rb:24:in `block (4 levels) in <top (required)>'

Here's my sessions_controller_spec.rb code:

require 'spec_helper'

describe SessionsController do
  before do
    @request.env["devise.mapping"] = Devise.mappings[:user]
  end

  describe 'POST #create' do
    context "when password is INCORRECT" do
      let!(:user) { FactoryGirl.create(:user, username: 'Example', password: 'Secr3t&$') }

      before(:each) do
        post :create, user: { username: 'Example', password: '' }
      end

      it { should set_the_flash[:alert].to('Invalid username or password.') }
      it { should respond_with(:redirect) }
      it { should redirect_to(:new_user_session) }
  end
end

Here's my spec_helper.rb code:

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

Any help would be much appreciated. Thanks!

jeffdill2
  • 3,968
  • 2
  • 30
  • 47
  • Can you double check that it is included in the test as well? I know it *should* be, based on your spec_helper, but I remember at least once when it was not automatically hooking up for me like you'd expect. – Sammy Larbi Feb 18 '15 at 18:18
  • There was a similar issue here: http://stackoverflow.com/a/17541915/3507417 – atw Feb 19 '15 at 16:17

1 Answers1

2

I know it is too late now but in case this helps someone else: you need to add setup_controller_for_warden to your before block, so it becomes:

before do
  @request.env["devise.mapping"] = Devise.mappings[:user]
  setup_controller_for_warden
end

Also it would be better to use the translation for you assertion. In rspec 3 format, the assertion should like this:

expect(flash[:alert]).to eq(I18n.t('devise.failure.invalid'))
pymkin
  • 4,304
  • 1
  • 21
  • 18
  • Thanks for the proper I18n check. If you're using custom authentication keys, you can parameterize it: `expect(flash[:alert]).to eq(I18n.t('devise.failure.invalid', authentication_keys: 'Login'))` – bjnord Apr 11 '18 at 13:19