CSC/ECE 517 Fall 2018/OSS E1848 Write unit tests for assignment team.rb: Difference between revisions
Line 72: | Line 72: | ||
=='''Conclusion'''== | =='''Conclusion'''== | ||
The testing framework in the assignment_team_spec.rb used unit tests to test the functionality of each action in the class. The mock instances are created at the beginning of the file, so that during each test they don't need to be constructed again. In order to test each unit case without depending on other functionalities, the mocked actions, as well as the desired returns are built inside different test cases, depending on the need of the case. | The testing framework in the assignment_team_spec.rb used unit tests to test the functionality of each action in the class. The mock instances are created at the beginning of the file, so that during each test they don't need to be constructed again. In order to test each unit case without depending on other functionalities, the mocked actions, as well as the desired returns are built inside different test cases, depending on the need of the case. | ||
In building the test framework, the key is to understand the input, output and the desired action of each functionality that we want to test. The Rspec test format also has a steep learning curve, but the test shows stable and robust result on the assignment_team.rb. | In building the test framework, the key is to understand the input, output and the desired action of each functionality that we want to test. The Rspec test format also has a steep learning curve, but the test shows stable and robust result on the assignment_team.rb. |
Revision as of 03:31, 2 November 2018
For this progect, the goal is to wirte up unit tests for assignment_team.rb
Introduction
Assignment_team.rb in Expertiza provides a method for student to assign a team and finish one assignment. Features come at the cost of complexity; this project is focused on creating the test methods to ensure that user interaction with the assignment interface remains stable and reliable.
Project Description
There are some examples for how this project create test methods for assignment_team.rb
Create team
In assignment_team.rb,there is a method return the team given the participant.
def self.team(participant) return nil if participant.nil? team = nil teams_users = TeamsUser.where(user_id: participant.user_id) return nil unless teams_users teams_users.each do |teams_user| team = Team.find(teams_user.team_id) return team if team.parent_id == participant.parent_id end nil end
Based on the above method, the test case can be created, and there are three different possible results could be generated.
- when the participant is nil
it "returns nil" do expect(AssignmentTeam.team(participant1)).to eq(nil) end
- when there are not team users records
it "returns nil" do allow(TeamsUser).to receive(:where).with(user_id: 1).and_return(nil) expect(AssignmentTeam.team(participant1)).to eq(nil) end
- when the participant is not nil and there exist team users records
it "returns the team given the participant" do allow(TeamsUser).to receive(:where).with(user_id: 1).and_return([team_user]) allow(Team).to receive(:find).with(1).and_return(team) expect(AssignmentTeam.team(participant1)).to eq(team) end
Remove team by id
The assignment_team.rb also provide a method which could remove team by id.
def self.remove_team_by_id(id) old_team = AssignmentTeam.find(id) old_team.destroy unless old_team.nil? end
Based on the above code, the test case can be created:
it "deletes a team geiven the team id" do old_team = assignment_team1 allow(AssignmentTeam).to receive(:find).with(1).and_return(old_team) allow(old_team).to receive(:destroy).and_return(old_team) expect(AssignmentTeam.remove_team_by_id(1)).to eq(old_team) end
Result
Our test cases has coverage:
There are in total XX many examples, and all of them get passed.
Conclusion
The testing framework in the assignment_team_spec.rb used unit tests to test the functionality of each action in the class. The mock instances are created at the beginning of the file, so that during each test they don't need to be constructed again. In order to test each unit case without depending on other functionalities, the mocked actions, as well as the desired returns are built inside different test cases, depending on the need of the case.
In building the test framework, the key is to understand the input, output and the desired action of each functionality that we want to test. The Rspec test format also has a steep learning curve, but the test shows stable and robust result on the assignment_team.rb.