added tests

This commit is contained in:
Hauke Klement
2015-02-23 12:44:22 +01:00
parent 11a0679b3a
commit f337f1c1a3
2 changed files with 30 additions and 11 deletions

View File

@ -7,20 +7,35 @@ describe ExercisesController do
describe 'POST #clone' do describe 'POST #clone' do
let(:request) { proc { post :clone, id: exercise.id } } let(:request) { proc { post :clone, id: exercise.id } }
before(:each) { request.call }
expect_assigns(exercise: Exercise) context 'when saving succeeds' do
before(:each) { request.call }
it 'clones the exercise' do expect_assigns(exercise: Exercise)
expect_any_instance_of(Exercise).to receive(:duplicate).with(hash_including(public: false, user: user)).and_call_original
expect { request.call }.to change(Exercise, :count).by(1) it 'clones the exercise' do
expect_any_instance_of(Exercise).to receive(:duplicate).with(hash_including(public: false, user: user)).and_call_original
expect { request.call }.to change(Exercise, :count).by(1)
end
it 'generates a new token' do
expect(Exercise.last.token).not_to eq(exercise.token)
end
expect_redirect
end end
it 'generates a new token' do context 'when saving fails' do
expect(Exercise.last.token).not_to eq(exercise.token) before(:each) do
end expect_any_instance_of(Exercise).to receive(:save).and_return(false)
request.call
end
expect_redirect expect_assigns(exercise: Exercise)
expect_flash_message(:danger)
expect_redirect(:exercise)
end
end end
describe 'POST #create' do describe 'POST #create' do

View File

@ -21,9 +21,13 @@ def expect_content_type(content_type)
end end
end end
def expect_flash_message(type, message) def expect_flash_message(type, message = nil)
it 'displays a flash message' do it 'displays a flash message' do
expect(flash[type]).to eq(message.is_a?(String) ? message : I18n.t(message)) if message
expect(flash[type]).to eq(message.is_a?(String) ? message : I18n.t(message))
else
expect(flash[type]).to be_present
end
end end
end end