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

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 92: Line 92:
</pre>
</pre>


Based on the above code, the test case can be created:
Based on the above code, the test case can be created, and there are three possible results:
*when current assignment team submitted files
<pre>
<pre>
   describe "has_submissions?" do
   it "returns true" do
 
    allow(assignment_team1).to receive(:submitted_files).and_return([double(:File)])
    context "when current assignment team submitted files" do
    expect(assignment_team1.has_submissions? ).to be true
      it "returns true" do
  end
        allow(assignment_team1).to receive(:submitted_files).and_return([double(:File)])
</pre>
        expect(assignment_team1.has_submissions? ).to be true
*when current assignment team did not submit files but submitted hyperlinks
      end
<pre>
    end
  it "returns true" do
 
    allow(assignment_team1).to receive(:submitted_hyperlinks).and_return([double(:Hyperlink)])
    context "when current assignment team did not submit files but submitted hyperlinks" do
    expect(assignment_team1.has_submissions? ).to be true
        it "returns true" do
  end
        allow(assignment_team1).to receive(:submitted_hyperlinks).and_return([double(:Hyperlink)])
</pre>
       
*when current assignment team did not submit either files or hyperlinks
        expect(assignment_team1.has_submissions? ).to be true
<pre>
        end
  it "returns false" do
      end
    expect(assignment_team1.has_submissions? ).to be false
 
  end
      context "when current assignment team did not submit either files or hyperlinks" do
        it "returns false" do
          expect(assignment_team1.has_submissions? ).to be false
 
        end
        # Write your test here!
      end
    end
</pre>
</pre>
This test can successfully test whether the team has submitted files/hyperlinks or not, because it creates a mock function, for the submitted_files function to receive a parameter - so that the team has hypothetically passed in a submitted file. The returning value being true shows that the has_submissions? test succeeded when the team receives a submitted file.
This test can successfully test whether the team has submitted files/hyperlinks or not, because it creates a mock function, for the submitted_files function to receive a parameter - so that the team has hypothetically passed in a submitted file. The returning value being true shows that the has_submissions? test succeeded when the team receives a submitted file.



Revision as of 17:48, 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

Return the topic chosen by the team

The assignment_team.rb offers a method that could return the topic chosen by the team.

  def topic
    SignedUpTeam.find_by(team_id: self.id, is_waitlisted: 0).try(:topic_id)
  end

Based on the above code, the test case can be created:

  describe "#topic" do
    it "returns the topic id chosen by this team" do
      allow(SignedUpTeam).to receive(:find_by).with(team_id:1, is_waitlisted: 0).and_return(signed_up_team1)
      expect(assignment_team1.topic).to eq(1)
    end
  end

This test examines the original codes ability to generate the topic that is chosen by the team, because it firstly goes to the mocked instances to look for a team. In this case, to match the mock instance that has created, we look for the team that has team_id as 1, and not being waitlisted. THe topic id is set to be 1 in the mock. This function returns 1, because it does return the topic id, for the team that has signed up for a topic.

Return whether the team has submission

The assignment_team.rb offers a method that could return either true or false, in terms of whether the team has submitted work or not

  def has_submissions?
    self.submitted_files.any? or self.submitted_hyperlinks.present?
  end

Based on the above code, the test case can be created, and there are three possible results:

  • when current assignment team submitted files
  it "returns true" do
    allow(assignment_team1).to receive(:submitted_files).and_return([double(:File)])
    expect(assignment_team1.has_submissions? ).to be true
  end
  • when current assignment team did not submit files but submitted hyperlinks
  it "returns true" do
    allow(assignment_team1).to receive(:submitted_hyperlinks).and_return([double(:Hyperlink)])
    expect(assignment_team1.has_submissions? ).to be true
  end
  • when current assignment team did not submit either files or hyperlinks
  it "returns false" do
    expect(assignment_team1.has_submissions? ).to be false
  end

This test can successfully test whether the team has submitted files/hyperlinks or not, because it creates a mock function, for the submitted_files function to receive a parameter - so that the team has hypothetically passed in a submitted file. The returning value being true shows that the has_submissions? test succeeded when the team receives a submitted file.

Similarly, we created a mock function for the team to receive a hyperlink using submitted_hyperlink. This second test case returning true shows the hyperlink being submitted is also detected by the function.

When there's no mock happening inside the function, no file or hyperlink will be passed in to the team. So has_submissions? returns false is what's being expected.

Result

Our test cases has coverage: 100%

There are in total 127 relevant lines, 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.