CSC/ECE 517 Fall 2018 - Project E1852 Write unit tests for participant.rb: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 105: Line 105:
====able_to_review====
====able_to_review====
<pre>
<pre>
 
  describe "#able_to_review" do
    it "returns whether the current participant has permission to review others' work" do
      expect(participant.able_to_review).to be false
    end
  end
</pre>
</pre>


====email====
====email====
<pre>
<pre>
 
  describe "#email" do
    it "sends an assignment registration email to current user" do
      allow(User).to receive(:find_by).with(id: nil).and_return(student)
      allow(Assignment).to receive(:find_by).with(id: nil).and_return(assignment)
      expect(participant.email(12_345, 'homepage').subject).to eq "You have been registered as a participant in the Assignment assignment"
      expect(participant.email(12_345, 'homepage').to[0]).to eq "expertiza.development@gmail.com"
      expect(participant.email(12_345, 'homepage').from[0]).to eq "expertiza.development@gmail.com"
    end
  end
</pre>
</pre>


====topic_name====
====topic_name====
<pre>
<pre>
    context "when the topic is nil or the topic name is empty" do
      it "returns em dash (—)" do
        expect(participant.topic_name).to eq "<center>&#8212;</center>"
      end
    end
</pre>


<pre>
    context "when the topic is not nil and the topic name is not empty" do
      it "returns the topic name" do
        allow(participant).to receive(:topic).and_return(topic2)
        expect(participant.topic_name).to eq "topic_name"
      end
    end
  end
</pre>
</pre>



Revision as of 00:40, 3 November 2018

Introduction

The objective for this project is to write unit tests using Rspec for participant.rb model, which is used to prepare data for participants enrolled in each course/assignment. By editing the participant_spct.rb, all class and instance methods are tested, and the path coverage is above 90% with all 97 lines covered.

Expertiza

Expertiza is an open source educational web application developed on Ruby on Rails framework. Using Expertiza, students can submit and peer-review learning objects such as articles, code, and websites. The source code is available on Github and it allows students to improve and maintain.

Behavior-DrivenDevelopment

In software engineering, behavior-driven development (BDD) is a software development process that emerged from test-driven development(TDD). The behavior-driven development combines the general techniques and principles of TDD with ideas from domain-driven design and object-oriented analysis and design to provide software development and management teams with shared tools and a shared process to collaborate on software development.

Rspec

RSpec is a 'Domain Specific Language' (DSL) testing tool written in Ruby to test Ruby code. It is a behavior-driven development (BDD) framework which is extensively used in the production applications. The basic idea behind this concept is that of Test Driven Development (TDD) where the tests are written first and the development is based on writing just enough code that will fulfill those tests followed by refactoring. It contains its own mocking framework that is fully integrated into the framework based upon JMock. The simplicity in the RSpec syntax makes it one of the popular testing tools for Ruby applications. The RSpec tool can be used by installing the rspec gem which consists of 3 other gems namely rspec-score, rspec-expectation and rspec-mock.

Problem Statement

The initial unit tests’ path coverage is only 36.08% with 35 lines covered and 62 lines missed for participant.rb, which are not enough. The unit test should be improved by making the path coverage of more than 90% and achieve the highest possible branch coverage.

Files Involved

A test file and two model files were modified for this project:

spec/models/participant_spec.rb

app/models/participant.rb

app/models/assignment_participant.rb

Steps

Expertiza Environment Setup

1. Install Virtual Box software form Oracle in PC

2. Download the Ubuntu-Expertiza image

3. Run the .ova file in Virtual Box

4. Run following commands in terminal

git clone http://github.com/user_name/expertiza

cd expertiza

./setup.sh

bundle install

rake db:migrate

rails s

5. Open up the browser and put localhost:3000 in the address bar. The expertiza login page should appear


Understand the Functionality

participant_spec.rb

participant_spec.rb is the file that test should be written in.

participant.rb

participant.rb is used to preparing data for participants enrolled in each course/assignment. It allows the user to review some information relate to the current course/assignment and execute operations such as sorting participant by their names and removing a current participant from the team. It also helps determine users' permission or authorization.

assignment_participant.rb

assignment_participant.rb is a model file related to participant.rb. AssignmentParticipant is a subclass of Participant class, and it overrides some methods of its superclass, which affects the process of writing test code.


Write Test Code

mock instance

All of the mock instances are listed at the beginning of the test file.

  let(:topic1) { build(:topic, topic_name: '') }
  let(:topic2) { build(:topic, topic_name: "topic_name") }
  let(:student) { build(:student, name: "John Smith", email: "sjohn@ncsu.edu", fullname: "Mr.John Smith") }
  let(:student1) { build(:student, name: "Alice") }
  let(:student2) { build(:student, name: "Bob") }
  let(:assignment) { build(:assignment, name: "assignment") }
  let(:participant) { build(:participant, user: student, assignment: assignment, can_review: false, handle: "nb") }
  let(:participant1) { build(:participant, user: student1) }
  let(:participant2) { build(:participant, user: student2) }
  let(:assignment_team) { build(:assignment_team, name: "ChinaNo1") }
  let(:team_user) { build(:team_user, team: assignment_team, user: student) }
  let(:response) { build(:response, response_map: response_map) }
  let(:response_map) { build(:review_response_map, assignment: assignment, reviewer: participant1, reviewee: assignment_team) }
  let(:questions) { build(:question) }
  let(:questionnaire) { build(:questionnaire) }

team

This case tests the team name of the current user will show up when team method is called

  describe "#team" do
    it "returns the first team of current user" do
      allow(TeamsUser).to receive(:find_by).with(user: student).and_return(team_user)
      expect(participant.team.name).to eq "ChinaNo1"
    end
  end

responses

  describe "#responses" do
    it "returns all responses this participant received" do
      allow(participant).to receive(:response).and_return(response)
      expect(participant.responses).to eq []
    end
  end

able_to_review

  describe "#able_to_review" do
    it "returns whether the current participant has permission to review others' work" do
      expect(participant.able_to_review).to be false
    end
  end

email

  describe "#email" do
    it "sends an assignment registration email to current user" do
      allow(User).to receive(:find_by).with(id: nil).and_return(student)
      allow(Assignment).to receive(:find_by).with(id: nil).and_return(assignment)
      expect(participant.email(12_345, 'homepage').subject).to eq "You have been registered as a participant in the Assignment assignment"
      expect(participant.email(12_345, 'homepage').to[0]).to eq "expertiza.development@gmail.com"
      expect(participant.email(12_345, 'homepage').from[0]).to eq "expertiza.development@gmail.com"
    end
  end

topic_name

    context "when the topic is nil or the topic name is empty" do
      it "returns em dash (—)" do
        expect(participant.topic_name).to eq "<center>—</center>"
      end
    end
    context "when the topic is not nil and the topic name is not empty" do
      it "returns the topic name" do
        allow(participant).to receive(:topic).and_return(topic2)
        expect(participant.topic_name).to eq "topic_name"
      end
    end
  end

sort_by_name


scores


get_permissions


get_authorization


handle


delete


force_delete


Test Result

With all class and instance methods tested, the path coverage is 99% with all 97 lines covered. Screenshot of the tests passing is shown below.

File:Jjj2.jpg

Full video of running the test can be found at https://youtu.be/PGyQAtbmI5w.

Reference

Behavior-DrivenDevelopment https://en.wikipedia.org/wiki/Behavior-driven_development

Expertiza https://expertiza.ncsu.edu/

Rspec Documentation https://relishapp.com/rspec

Github (Expertiza) https://github.com/expertiza/expertiza