diff --git a/spec/concerns/submission_scoring_spec.rb b/spec/concerns/submission_scoring_spec.rb index cc30fe56..c1394b92 100644 --- a/spec/concerns/submission_scoring_spec.rb +++ b/spec/concerns/submission_scoring_spec.rb @@ -5,12 +5,15 @@ class Controller < AnonymousController end describe SubmissionScoring do + before(:all) do + @submission = FactoryGirl.create(:submission, cause: 'submit') + end + let(:controller) { Controller.new } - let(:submission) { FactoryGirl.create(:submission, cause: 'submit') } before(:each) { controller.instance_variable_set(:@current_user, FactoryGirl.create(:external_user)) } describe '#score_submission' do - let(:score_submission) { Proc.new { controller.score_submission(submission) } } + let(:score_submission) { Proc.new { controller.score_submission(@submission) } } before(:each) { score_submission.call } it 'assigns @assessor' do @@ -22,14 +25,14 @@ describe SubmissionScoring do end it 'executes the teacher-defined test cases' do - submission.collect_files.select(&:teacher_defined_test?).each do |file| - expect_any_instance_of(DockerClient).to receive(:execute_test_command).with(submission, file.name_with_extension).and_return({}) + @submission.collect_files.select(&:teacher_defined_test?).each do |file| + expect_any_instance_of(DockerClient).to receive(:execute_test_command).with(@submission, file.name_with_extension).and_return({}) end score_submission.call end it 'updates the submission' do - expect(submission).to receive(:update).with(score: anything) + expect(@submission).to receive(:update).with(score: anything) score_submission.call end end diff --git a/spec/controllers/errors_controller_spec.rb b/spec/controllers/errors_controller_spec.rb index efbe3d1a..e760f2ed 100644 --- a/spec/controllers/errors_controller_spec.rb +++ b/spec/controllers/errors_controller_spec.rb @@ -61,7 +61,7 @@ describe ErrorsController do end describe 'GET #index' do - let!(:errors) { FactoryGirl.create_pair(:error) } + before(:all) { FactoryGirl.create_pair(:error) } before(:each) { get :index, execution_environment_id: execution_environment.id } expect_assigns(execution_environment: :execution_environment) diff --git a/spec/controllers/execution_environments_controller_spec.rb b/spec/controllers/execution_environments_controller_spec.rb index 7f195b28..27be49e3 100644 --- a/spec/controllers/execution_environments_controller_spec.rb +++ b/spec/controllers/execution_environments_controller_spec.rb @@ -72,7 +72,7 @@ describe ExecutionEnvironmentsController do end describe 'GET #index' do - let!(:execution_environments) { FactoryGirl.create_pair(:ruby) } + before(:all) { FactoryGirl.create_pair(:ruby) } before(:each) { get :index } expect_assigns(execution_environments: ExecutionEnvironment.all) diff --git a/spec/controllers/exercises_controller_spec.rb b/spec/controllers/exercises_controller_spec.rb index ceda5a25..9302b983 100644 --- a/spec/controllers/exercises_controller_spec.rb +++ b/spec/controllers/exercises_controller_spec.rb @@ -1,12 +1,12 @@ require 'rails_helper' describe ExercisesController do - let(:exercise) { FactoryGirl.create(:fibonacci) } + let(:exercise) { FactoryGirl.create(:dummy) } let(:user) { FactoryGirl.create(:admin) } before(:each) { allow(controller).to receive(:current_user).and_return(user) } describe 'POST #create' do - let(:exercise_attributes) { FactoryGirl.build(:fibonacci).attributes } + let(:exercise_attributes) { FactoryGirl.build(:dummy).attributes } context 'with a valid exercise' do let(:request) { Proc.new { post :create, exercise: exercise_attributes } } @@ -61,7 +61,7 @@ describe ExercisesController do expect_assigns(exercise: :exercise) it 'destroys the exercise' do - exercise = FactoryGirl.create(:fibonacci) + exercise = FactoryGirl.create(:dummy) expect { delete :destroy, id: exercise.id }.to change(Exercise, :count).by(-1) end @@ -102,8 +102,8 @@ describe ExercisesController do end describe 'GET #index' do - let!(:exercises) { FactoryGirl.create_pair(:fibonacci) } let(:scope) { Pundit.policy_scope!(user, Exercise) } + before(:all) { FactoryGirl.create_pair(:dummy) } before(:each) { get :index } expect_assigns(exercises: :scope) @@ -195,7 +195,7 @@ describe ExercisesController do describe 'PUT #update' do context 'with a valid exercise' do - let(:exercise_attributes) { FactoryGirl.build(:fibonacci).attributes } + let(:exercise_attributes) { FactoryGirl.build(:dummy).attributes } before(:each) { put :update, exercise: exercise_attributes, id: exercise.id } expect_assigns(exercise: Exercise) diff --git a/spec/controllers/file_types_controller_spec.rb b/spec/controllers/file_types_controller_spec.rb index 5da53424..ed8ca675 100644 --- a/spec/controllers/file_types_controller_spec.rb +++ b/spec/controllers/file_types_controller_spec.rb @@ -53,7 +53,7 @@ describe FileTypesController do end describe 'GET #index' do - let!(:file_types) { FactoryGirl.create_pair(:dot_rb) } + before(:all) { FactoryGirl.create_pair(:dot_rb) } before(:each) { get :index } expect_assigns(file_types: FileType.all) diff --git a/spec/controllers/hints_controller_spec.rb b/spec/controllers/hints_controller_spec.rb index a881c643..7c47fc8a 100644 --- a/spec/controllers/hints_controller_spec.rb +++ b/spec/controllers/hints_controller_spec.rb @@ -55,7 +55,7 @@ describe HintsController do end describe 'GET #index' do - let!(:hints) { FactoryGirl.create_pair(:ruby_syntax_error) } + before(:all) { FactoryGirl.create_pair(:ruby_syntax_error) } before(:each) { get :index, execution_environment_id: execution_environment.id } expect_assigns(execution_environment: :execution_environment) diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb index 4012d5ee..79f53566 100644 --- a/spec/controllers/sessions_controller_spec.rb +++ b/spec/controllers/sessions_controller_spec.rb @@ -27,7 +27,7 @@ describe SessionsController do end describe 'POST #create_through_lti' do - let(:exercise) { FactoryGirl.create(:fibonacci) } + let(:exercise) { FactoryGirl.create(:dummy) } let(:nonce) { SecureRandom.hex } context 'without OAuth parameters' do @@ -125,7 +125,7 @@ describe SessionsController do describe 'GET #destroy_through_lti' do let(:request) { Proc.new { get :destroy_through_lti, consumer_id: consumer.id, submission_id: submission.id } } - let(:submission) { FactoryGirl.create(:submission) } + let(:submission) { FactoryGirl.create(:submission, exercise: FactoryGirl.create(:dummy)) } before(:each) do session[:consumer_id] = consumer.id diff --git a/spec/controllers/submissions_controller_spec.rb b/spec/controllers/submissions_controller_spec.rb index 10c9c600..b47edf59 100644 --- a/spec/controllers/submissions_controller_spec.rb +++ b/spec/controllers/submissions_controller_spec.rb @@ -57,7 +57,7 @@ describe SubmissionsController do end describe 'GET #index' do - let!(:submissions) { FactoryGirl.create_pair(:submission) } + before(:all) { FactoryGirl.create_pair(:submission) } before(:each) { get :index } expect_assigns(submissions: Submission.all) diff --git a/spec/controllers/teams_controller_spec.rb b/spec/controllers/teams_controller_spec.rb index 8c817b2c..dc823afe 100644 --- a/spec/controllers/teams_controller_spec.rb +++ b/spec/controllers/teams_controller_spec.rb @@ -50,7 +50,7 @@ describe TeamsController do end describe 'GET #index' do - let!(:teams) { FactoryGirl.create_pair(:team) } + before(:all) { FactoryGirl.create_pair(:team) } before(:each) { get :index } expect_assigns(teams: Team.all) diff --git a/spec/factories/exercise.rb b/spec/factories/exercise.rb index 6d009e41..dbbacd57 100644 --- a/spec/factories/exercise.rb +++ b/spec/factories/exercise.rb @@ -32,6 +32,14 @@ FactoryGirl.define do end end + factory :dummy, class: Exercise do + created_by_teacher + description 'Dummy' + association :execution_environment, factory: :ruby + instructions + title 'Dummy' + end + factory :even_odd, class: Exercise do created_by_teacher description 'Implement two methods even and odd which return whether a given number is even or odd, respectively.' diff --git a/spec/models/submission_spec.rb b/spec/models/submission_spec.rb index ffdd14a0..a66f361e 100644 --- a/spec/models/submission_spec.rb +++ b/spec/models/submission_spec.rb @@ -1,29 +1,30 @@ require 'rails_helper' describe Submission do - let(:submission) { Submission.create } + before(:all) do + @submission = FactoryGirl.create(:submission) + end it 'validates the presence of a cause' do - expect(submission.errors[:cause]).to be_present + expect(Submission.create.errors[:cause]).to be_present end it 'validates the presence of an exercise' do - expect(submission.errors[:exercise_id]).to be_present + expect(Submission.create.errors[:exercise_id]).to be_present end it 'validates the presence of a user' do - expect(submission.errors[:user_id]).to be_present - expect(submission.errors[:user_type]).to be_present + expect(Submission.create.errors[:user_id]).to be_present + expect(Submission.create.errors[:user_type]).to be_present end %w[download render run test].each do |action| describe "##{action}_url" do - let(:submission) { FactoryGirl.create(:submission) } - let(:url) { submission.send(:"#{action}_url") } + let(:url) { @submission.send(:"#{action}_url") } it "starts like the #{action} path" do filename = File.basename(__FILE__) - expect(url).to start_with(Rails.application.routes.url_helpers.send(:"#{action}_submission_path", submission, filename).sub(filename, '')) + expect(url).to start_with(Rails.application.routes.url_helpers.send(:"#{action}_submission_path", @submission, filename).sub(filename, '')) end it 'ends with a placeholder' do diff --git a/spec/policies/code_ocean/file_policy_spec.rb b/spec/policies/code_ocean/file_policy_spec.rb index fd467f83..80e5ad53 100644 --- a/spec/policies/code_ocean/file_policy_spec.rb +++ b/spec/policies/code_ocean/file_policy_spec.rb @@ -3,39 +3,41 @@ require 'rails_helper' describe CodeOcean::FilePolicy do subject { CodeOcean::FilePolicy } - let(:file) { FactoryGirl.build(:file) } - let(:exercise) { FactoryGirl.create(:fibonacci) } - let(:submission) { FactoryGirl.create(:submission) } - permissions :create? do context 'as part of an exercise' do - before(:each) { file.context = exercise } + before(:all) do + @exercise = FactoryGirl.create(:fibonacci) + @file = @exercise.files.first + end it 'grants access to admins' do - expect(subject).to permit(FactoryGirl.build(:admin), file) + expect(subject).to permit(FactoryGirl.build(:admin), @file) end it 'grants access to authors' do - expect(subject).to permit(exercise.author, file) + expect(subject).to permit(@exercise.author, @file) end it 'does not grant access to all other users' do [:external_user, :teacher].each do |factory_name| - expect(subject).not_to permit(FactoryGirl.build(factory_name), file) + expect(subject).not_to permit(FactoryGirl.build(factory_name), @file) end end end context 'as part of a submission' do - before(:each) { file.context = submission } + before(:all) do + @submission = FactoryGirl.create(:submission) + @file = @submission.files.first + end it 'grants access to authors' do - expect(subject).to permit(submission.author, file) + expect(subject).to permit(@submission.author, @file) end it 'does not grant access to all other users' do [:admin, :external_user, :teacher].each do |factory_name| - expect(subject).not_to permit(FactoryGirl.build(factory_name), file) + expect(subject).not_to permit(FactoryGirl.build(factory_name), @file) end end end @@ -43,29 +45,34 @@ describe CodeOcean::FilePolicy do permissions :destroy? do context 'as part of an exercise' do - before(:each) { file.context = exercise } + before(:all) do + @exercise = FactoryGirl.create(:fibonacci) + @file = @exercise.files.first + end it 'grants access to admins' do - expect(subject).to permit(FactoryGirl.build(:admin), file) + expect(subject).to permit(FactoryGirl.build(:admin), @file) end it 'grants access to authors' do - expect(subject).to permit(exercise.author, file) + expect(subject).to permit(@exercise.author, @file) end it 'does not grant access to all other users' do [:external_user, :teacher].each do |factory_name| - expect(subject).not_to permit(FactoryGirl.build(factory_name), file) + expect(subject).not_to permit(FactoryGirl.build(factory_name), @file) end end end context 'as part of a submission' do - before(:each) { file.context = submission } + before(:all) do + @file = FactoryGirl.create(:submission).files.first + end it 'does not grant access to anyone' do [:admin, :external_user, :teacher].each do |factory_name| - expect(subject).not_to permit(FactoryGirl.build(factory_name), file) + expect(subject).not_to permit(FactoryGirl.build(factory_name), @file) end end end diff --git a/spec/policies/exercise_policy_spec.rb b/spec/policies/exercise_policy_spec.rb index e11a06f3..624c93f8 100644 --- a/spec/policies/exercise_policy_spec.rb +++ b/spec/policies/exercise_policy_spec.rb @@ -4,7 +4,7 @@ describe ExercisePolicy do subject { ExercisePolicy } before(:all) do - @exercise = FactoryGirl.build(:fibonacci, team: FactoryGirl.create(:team)) + @exercise = FactoryGirl.build(:dummy, team: FactoryGirl.create(:team)) end [:create?, :index?, :new?].each do |action| @@ -67,7 +67,7 @@ describe ExercisePolicy do [@admin, @teacher].each do |user| [true, false].each do |public| [@team, nil].each do |team| - FactoryGirl.create(:fibonacci, public: public, team: team, user_id: user.id, user_type: InternalUser.class.name) + FactoryGirl.create(:dummy, public: public, team: team, user_id: user.id, user_type: InternalUser.class.name) end end end diff --git a/spec/policies/submission_policy_spec.rb b/spec/policies/submission_policy_spec.rb index d036696a..d04059a7 100644 --- a/spec/policies/submission_policy_spec.rb +++ b/spec/policies/submission_policy_spec.rb @@ -19,7 +19,7 @@ describe SubmissionPolicy do it 'grants access to authors' do user = FactoryGirl.create(:external_user) - expect(subject).to permit(user, FactoryGirl.build(:submission, user_id: user.id, user_type: user.class.name)) + expect(subject).to permit(user, FactoryGirl.build(:submission, exercise: Exercise.new, user_id: user.id, user_type: user.class.name)) end end end