diff --git a/spec/controllers/exercises_controller_spec.rb b/spec/controllers/exercises_controller_spec.rb index 43dada43..dca3103b 100644 --- a/spec/controllers/exercises_controller_spec.rb +++ b/spec/controllers/exercises_controller_spec.rb @@ -313,4 +313,30 @@ describe ExercisesController do expect_template(:edit) end end + + describe 'redirect after submit' do + + let!(:submission_with_created_at_to_receive_feedback) { FactoryGirl.create(:submission, exercise_id: exercise.id, user_id: user.id, user_type: user.class.name, created_at: (10 - (user.id % 10))) } + + let(:exercise_with_user_feedbacks) { FactoryGirl.build(:dummy_with_user_feedbacks, submission: submission_with_created_at_to_receive_feedback) } + let(:exercise_with_21_user_feedbacks) { FactoryGirl.build(:dummy_with_user_feedbacks, user_exercise_feedback_count: 21) } + + + + it 'with less than maximal user feedbacks' do + exercise_with_user_feedbacks.send(:redirect_after_submit) + expect_redirect(new_user_exercise_feedback_path(user_exercise_feedback: {exercise_id: @exercise.id})) + end + + it 'with more than maximal user feedbacks' do + exercise_with_user_feedbacks.send(:redirect_after_submit) + expect_redirect(new_user_exercise_feedback_path(user_exercise_feedback: {exercise_id: @exercise.id})) + end + + + + expect_redirect(Exercise.last) + + end + end diff --git a/spec/factories/exercise.rb b/spec/factories/exercise.rb index ea588faf..9d8ac5b1 100644 --- a/spec/factories/exercise.rb +++ b/spec/factories/exercise.rb @@ -38,6 +38,24 @@ FactoryGirl.define do association :execution_environment, factory: :ruby instructions title 'Dummy' + + factory :dummy_with_user_feedbacks do + # user_feedbacks_count is declared as a transient attribute and available in + # attributes on the factory, as well as the callback via the evaluator + transient do + user_feedbacks_count 5 + end + + # the after(:create) yields two values; the exercise instance itself and the + # evaluator, which stores all values from the factory, including transient + # attributes; `create_list`'s second argument is the number of records + # to create and we make sure the user_exercise_feedback is associated properly to the exercise + after(:create) do |exercise, evaluator| + create_list(:user_exercise_feedback, evaluator.user_feedbacks_count, exercise: exercise) + end + + end + end factory :even_odd, class: Exercise do diff --git a/spec/factories/user_exercise_feedback.rb b/spec/factories/user_exercise_feedback.rb new file mode 100644 index 00000000..b5a376cd --- /dev/null +++ b/spec/factories/user_exercise_feedback.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :user_exercise_feedback, class: UserExerciseFeedback do + created_by_external_user + feedback_text 'Most suitable exercise ever' + end + +end \ No newline at end of file