CSC/ECE 517 Fall 2016/E1671. Unit Tests for participants.rb Hierarchy

From Expertiza_Wiki
Revision as of 18:02, 29 October 2016 by Kvatwan (talk | contribs)
Jump to navigation Jump to search

This wiki page is for the description of changes made under E1671 OSS assignment for Fall 2016, CSC/ECE 517.


Background

Expertiza is an open source web based peer review system developed and maintained by students and faculty members at North Carolina State University. It enables students enrolled in a particular course to form online teams and complete assignments.

Problem

The participant, course_participant and assignment_pariticipant models does not have any unit tests.

What to do

  • Create a Fatcory for assignment participant and course participant model.
  • Test the relationships and validations
  • Write model spec for major instances

Files modified/created

  • spec/factories/factories.rb
  • spec/models/participant_spec.rb
  • spec/models/assignment_participant_spec.rb
  • spec/models/course_participant_spec.rb

Unit test Description

Unit tests are used to test the functionality of methods in the model. These tests can be based on either test-driven development (TDD) or behavior-driven development (BDD). BDD is preferred for testing because it easy to understand compared to the more specialized TDD.<ref>https://www.toptal.com/freelance/your-boss-won-t-appreciate-tdd-try-bdd</ref> In this project, BDD is used to test the functionality using RSpec. RSpec is a testing tool for ruby using the BDD approach.<ref>https://semaphoreci.com/community/tutorials/getting-started-with-rspec</ref>


Course Participant Unit Test

The Course Participant class is to show the participation of the student into a particular course. We have used the rspec framework to test the course participant class. The course participant class is inherited from the participant class associated with the course class through belongs_to relationship. We have tested the four different methods of the class course participant.The first method to test is the copy method. The copy method checks if an assignment participant exists for the corresponding parent_id. If it exists then a null is returned otherwise a new object for assignment participant is returned. We check both the functionality of the methods using two different tests for the method class. The next important method is the import method. The import method consists of four different scenarios and thus we have written test for testing four different scenarios for the import method. The import method takes a record field and input and checks for various condition on the record field and creates the course participant object. Below is a short snipped of the various tests written for the copy and import method

 describe "#copy" do

    it "create a copy of participant" do
      assignment = build(:assignment)
      course_participant = build(:course_participant)
      assign_part = build(:assignment_participant)

      allow(AssignmentParticipant).to receive(:create).and_return(assign_part)
      allow(assign_part).to receive(:set_handle).and_return(true)

      expect(course_participant.copy(assignment.id)).to be_an_instance_of(AssignmentParticipant)

    end

    it "returns nil if copy exist" do
      assignment = build(:assignment)
      course_participant = build(:course_participant)
      assign_part = build(:assignment_participant)

      allow(AssignmentParticipant).to receive(:where).and_return(AssignmentParticipant)
      allow(AssignmentParticipant).to receive(:first).and_return(assign_part)
      allow(assign_part).to receive(:set_handle).and_return(true)

      expect(course_participant.copy(assignment.id)).to be_nil
    end
  end
  describe "#import" do

    it "raise error if record is empty" do
      row = []
      expect {CourseParticipant.import(row,nil,nil,nil)}.to raise_error("No user id has been specified.")
    end

    it "raise error if record does not have enough items " do
      row = ["user_name","user_fullname","name@email.com"]
      expect {CourseParticipant.import(row,nil,nil,nil)}.to raise_error("The record containing #{row[0]} does not have enough items.")
    end

    it "raise error if course with id not found" do
      course = build(:course)
      session = {}
      row =[]
      allow(Course).to receive(:find).and_return(nil)
      allow(session[:user]).to receive(:id).and_return(1)
      row = ["user_name","user_fullname","name@email.com","user_role_name","user_parent_name"]
      expect {CourseParticipant.import(row,nil,session,2)}.to raise_error("The course with the id \"2\" was not found.")
    end

    it "creates course participant form record" do
      course = build(:course)
      session = {}
      row =[]
      allow(Course).to receive(:find).and_return(course)
      allow(session[:user]).to receive(:id).and_return(1)
      row = ["user_name","user_fullname","name@email.com","user_role_name","user_parent_name"]
      course_part = CourseParticipant.import(row,nil,session,2)
      expect(course_part).to be_an_instance_of(CourseParticipant)
    end

  end

Run the test

The tests can be run on the terminal from inside the expertiza folder using following commands:

For participant:

 rspec spec/models/participant_spec.rb

For assignment_participant:

 rspec spec/models/assignment_participant_spec.rb

For participant:

 rspec spec/models/assignment_participant_spec.rb

References

<references /> 1. https://www.toptal.com/freelance/your-boss-won-t-appreciate-tdd-try-bdd

2. https://semaphoreci.com/community/tutorials/getting-started-with-rspec