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
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_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)
expect_assigns(exercise: Exercise)
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
it 'generates a new token' do
expect(Exercise.last.token).not_to eq(exercise.token)
end
context 'when saving fails' do
before(:each) do
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
describe 'POST #create' do

View File

@ -21,9 +21,13 @@ def expect_content_type(content_type)
end
end
def expect_flash_message(type, message)
def expect_flash_message(type, message = nil)
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