Spec the failure cases for PreferencesController#update

This commit is contained in:
Robert Speicher 2015-06-10 18:19:06 -04:00
parent fb5271ddf1
commit 1562f017b6
2 changed files with 27 additions and 6 deletions

View File

@ -5,10 +5,15 @@ class Profiles::PreferencesController < Profiles::ApplicationController
end
def update
if @user.update_attributes(preferences_params)
flash[:notice] = 'Preferences saved.'
else
# TODO (rspeicher): There's no validation on these values, so can it fail?
begin
if @user.update_attributes(preferences_params)
flash[:notice] = 'Preferences saved.'
else
flash[:alert] = 'Failed to save preferences.'
end
rescue ArgumentError => e
# Raised when `dashboard` is given an invalid value.
flash[:alert] = "Failed to save preferences (#{e.message})."
end
respond_to do |format|

View File

@ -51,8 +51,24 @@ describe Profiles::PreferencesController do
end
end
context 'on unsuccessful update' do
# TODO (rspeicher): Can this happen?
context 'on failed update' do
it 'sets the flash' do
expect(user).to receive(:update_attributes).and_return(false)
go
expect(flash[:alert]).to eq('Failed to save preferences.')
end
end
context 'on invalid dashboard setting' do
it 'sets the flash' do
prefs = {dashboard: 'invalid'}
go params: prefs
expect(flash[:alert]).to match(/\AFailed to save preferences \(.+\)\.\z/)
end
end
context 'as js' do