CSC/ECE 517 Fall 2018/OSS E1848 Write unit tests for assignment team.rb

From Expertiza_Wiki
Revision as of 18:07, 1 November 2018 by Gli22 (talk | contribs)
Jump to navigation Jump to search

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.

  • 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

Conclusion