E1848 Write unit tests for assignment team
For this progect, the goal is to wirte up unit tests for assignment_team.rb
Project Introduction
assignment_team.rb is the child class of team.rb. It is used to manage student teams in assignments. There are not enough unit tests for this model in expertiza. The following tests are added to assignment_team.rb in this project.
Unit Tests
Unit Test Tool
Rspec
Unit Test File
expertiza/spec/models/assignment_team_spec.rb
Unit Test Steps
- Create unit test cases in the assignment_team_spec.rb
- run rspec spec/models/assignment_team_spec.rb under expertiza directory
- the rspec outputs the number of test cases that are passed and those that are failed.
Unit Test Cases
Testing has_submissions?
3 test cases are designed to test this method:
1.The team doesn't submit any file or link.
let(:team2) { build(:assignment_team, id: 2, parent_id: 1, name: "team2", submitted_hyperlinks: "") } it "no file or no link" do expect(team2.has_submissions?).to eq(false) end
2.The team submits a link instead of files.
let(:team1) { build(:assignment_team, id: 1, parent_id: 1, name: "team1", submitted_hyperlinks: "http://example.com") } it "submitted hyperlinks" do expect(team1.has_submissions?).to eq(true) end
3. The team submits some files. Stub is used to emulate the scenario that the team has submitted a file.
it "returns true" do allow(team2).to receive(:submitted_files).and_return([double(:File)]) expect(team2.has_submissions?).to be true end
Testing participants
1. No participants in this team
it '#no participant' do expect(team1.participants).to eq([]) end
2. add some participants and get all the participants
let(:student1) { build(:student, id: 1, name: 'johns', fullname: 'johns franklin') } let(:student2) { build(:student, id: 2, name: 'kate', fullname: 'kate moss') } let(:par1) { build(:participant, id: 1, parent_id: 1, user_id: 1) } let(:par2) { build(:participant, id: 2, parent_id: 1, user_id: 2) } it '#1 participant' do allow(AssignmentTeam).to receive(:users).with(id: team1.id).and_return([student1,student2]) expect(team1.participants).to eq([par1, par2]) end
Testing add_participant
it '#add participants' do expect(team1.add_participant(ass1.id, par1)).to be_instance_of(AssignmentParticipant) end
Testing the delete and destroy
it '#delete' do expect(team1.delete).to be_instance_of(AssignmentTeam) end
it '#destroy' do expect(team1.destroy).to be_instance_of(AssignmentTeam) end
Testing members
it '#get_first_member' do allow(AssignmentTeam).to receive(:find_by).with(id: team1.id).and_return(team1) allow(team1).to receive(:participants).and_return([par1, par2]) expect(AssignmentTeam.get_first_member(team1.id)).to eq(par1) end
Testing import and export
1. import a team to a non-existing assignment and expect an exception
it '#import an nonexisting assignment id' do row = {teamname: "hello_world", teammembers: %w[johns kate]} options = {has_teamname: "true_first"} expect { AssignmentTeam.import(row, 99_999, options) }.to raise_error(ImportError) end
2. export teams into a csv file
it '#export' do options = {} csv = CSV.open("assignment_team_export.csv", "w") assignment_id = ass1.id allow(AssignmentTeam).to receive(:where).with(parent_id: assignment_id).and_return([team1, team2, team3]) allow(TeamsUser).to receive(:where).and_return([par1, par2]) expect(AssignmentTeam.export(csv, assignment_id, options)).to be_instance_of(CSV) end
Testing copy
it '#copy' do allow(TeamsUser).to receive(:where).with(team_id: team1.id).and_return([par1]) expect(team1.copy(course1.id)).to eq([par1]) end