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!