added tests

This commit is contained in:
Hauke Klement
2015-02-26 10:40:51 +01:00
parent aada0e1c9a
commit 8dfc92d8c9

View File

@ -1,9 +1,7 @@
require 'rails_helper'
describe Submission do
before(:all) do
@submission = FactoryGirl.create(:submission)
end
let(:submission) { FactoryGirl.create(:submission, exercise: FactoryGirl.create(:dummy)) }
it 'validates the presence of a cause' do
expect(described_class.create.errors[:cause]).to be_present
@ -20,11 +18,11 @@ describe Submission do
[:download, :render, :run, :test].each do |action|
describe "##{action}_url" do
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
@ -33,19 +31,83 @@ describe Submission do
end
end
describe '#main_file' do
let(:submission) { FactoryGirl.create(:submission) }
it "returns the submission's main file" do
expect(submission.main_file).to be_a(CodeOcean::File)
expect(submission.main_file.main_file?).to be true
end
end
describe '#normalized_score' do
context 'with a score' do
let(:submission) { FactoryGirl.create(:submission) }
before(:each) { submission.score = submission.exercise.maximum_score / 2 }
it 'returns the score as a value between 0 and 1' do
expect(0..1).to include(submission.normalized_score)
end
end
context 'without a score' do
before(:each) { submission.score = nil }
it 'returns nil' do
expect(submission.normalized_score).to be nil
end
end
end
describe '#percentage' do
context 'with a score' do
let(:submission) { FactoryGirl.create(:submission) }
before(:each) { submission.score = submission.exercise.maximum_score / 2 }
it 'returns the score expressed as a percentage' do
expect(0..100).to include(submission.percentage)
end
end
context 'without a score' do
before(:each) { submission.score = nil }
it 'returns nil' do
expect(submission.percentage).to be nil
end
end
end
[:score, :stop].each do |action|
describe "##{action}_url" do
let(:url) { @submission.send(:"#{action}_url") }
let(:url) { submission.send(:"#{action}_url") }
it "corresponds to the #{action} path" do
expect(url).to eq(Rails.application.routes.url_helpers.send(:"#{action}_submission_path", @submission))
expect(url).to eq(Rails.application.routes.url_helpers.send(:"#{action}_submission_path", submission))
end
end
end
describe '#siblings' do
let(:siblings) { described_class.find_by(user: user).siblings }
let(:user) { FactoryGirl.create(:external_user) }
before(:each) do
10.times.each_with_index do |_, index|
FactoryGirl.create(:submission, exercise: submission.exercise, user: (index.even? ? user : FactoryGirl.create(:external_user)))
end
end
it "returns all the creator's submissions for the same exercise" do
expect(siblings).to be_an(ActiveRecord::Relation)
expect(siblings.map(&:exercise).uniq).to eq([submission.exercise])
expect(siblings.map(&:user).uniq).to eq([user])
end
end
describe '#to_s' do
it "equals the class' model name" do
expect(@submission.to_s).to eq(described_class.model_name.human)
expect(submission.to_s).to eq(described_class.model_name.human)
end
end
end