CSC/ECE 517 Fall 2016/E1644. Refactor and test Teams Controller

From Expertiza_Wiki
Revision as of 04:14, 29 October 2016 by Bsinha (talk | contribs) (→‎References)
Jump to navigation Jump to search

E1644: Refactor and Test 'Teams_Controller' Controller

This page provides a brief description of the Expertiza project. The project is aimed at refactoring and testing the 'Teams_Controller' Controller, which contains the methods to create new teams, list the existing teams, create new teams by inheriting existing teams, deleting existing teams and creating multiple teams at once by assigning them random topics. The project entailed, refactoring some part of the controller to improve the readability of code and then creating test cases in RSpec to verify the methods it possesses.

Introduction to Expertiza

Expertiza is a peer review based system which provides incremental learning from the class. This project has been developed together by faculty and students using Ruby on Rails framework. Expertiza allows the instructor to create, edit and delete assignments, create new assignment topics, assign them to a particular class or selected students, have students work on teams and then review each other's assignments at the end. For the students, they can signup for topics, form teams, and submit their projects and assignments. Students then review the work done by other students and give suggestions to improve. Teams after reviews are allotted scores and they can refer to the peer comments to further improve their work. It also supports submission of different file types for assignments, including the URLs and wiki pages.

Project Description

Teams_Controller primarily handles the team creation, deletion and other behaviours. Most of those are called from the instructor’s view. When manual testing is done, most of the methods can be called by clicking the “Create teams” icon from both assignments and courses. The following tasks have been performed as per the requirements.

As a part of the project, some GUI's changes had to be made because they did not work the way they should have, some minor issues in the code were fixed and tests were written in RSpec for the methods delete, create, create teams, list and inherit.

Create Method

The method Create is called when an instructor tries to create a team manually. This works for both, creating assignment teams and course teams. Assignment teams are teams made to do a particular assignment together and Course Teams are teams which are made for the whole course.

Following Test Cases were written and executed in RSpec to test this method:

  • To verify that the Instructor is able to create course teams.
  • To verify that the Instructor is able to create assignment teams.

Delete Method

The method Delete is called when an instructor tries to delete a team manually. This works for both, deleting assignment teams and course teams.

Following Test Cases were written and executed in RSpec to test this method:

  • To verify that the Instructor is able to delete course teams.
  • To verify that the Instructor is able to delete assignment teams.

List Method

The method List lists all the team nodes and the instructor is able to expand each team node to see the user nodes as well.

Following Test Cases were written and executed in RSpec to test this method:

  • To check that all teams are being listed in the view.

Inherit Method

The method Inherit inherits teams from course to assignments, but the “Inherit Teams From Course” option should not display when either 1) we are editing a course object or 2) the current assignment object does not have a course associated with it.

Following Test Cases were written and executed in RSpec to test this method:

  • To check that inherit teams is displayed while creating an assignment team.
  • To check that inherit teams is not displayed while creating a course team.
  • To check that inherit teams is not displayed while creating teams for an assignment without a course.

Refactoring

Refactoring[[1]] is restructuring of code without the need of changing any external behavior. It reduces complexity and improves readability. It also becomes easy to extend the application with respect to different modules and their functionalities. Some common techniques to refactor are:

  • Moving methods to appropriate modules
  • Breaking methods into more meaningful functionality
  • Creating more generalized code.
  • Renaming methods and variable.
  • Inheritance

As the part of the project, the variable @signUps in the delete method in the teams_controller was changed to snake case, to improve readability of code.

Changes to View

To fix the view for inherit teams functionality, changes to the view were made.

Initially, the code was:

 Inherit Teams From Course
 Use the teams that are currently defined for the course.
<%= form_tag :action => 'inherit' do %>
  <%= hidden_field_tag 'id', @parent.id %>
  <%= submit_tag "Inherit" %>

Which was changed to:

 <% if not @parent.instance_of?(Course) and not @parent.course.nil? %>
   Inherit Teams From Course 
   Use the teams that are currently defined for the course.
  <%= form_tag :action => 'inherit' do %>
    <%= hidden_field_tag 'id', @parent.id %>

   <%= submit_tag "Inherit" %>
 <% end %>

To fix the view, in the new.html.erb file of teams, we enclose the inherit teams section in an if condition that checks that the parent of the team is not a Course and it is not an Assignment whose course is nil.

Testing the Teams_Controller

Testing Inherit Method

To test that while creating an assignment team, the inherit teams section should be displayed, we login as instructor got to list page and click on Create Team Link. On this page we test that it should contain the string 'Inherit Teams From Course'.

 it 'should display inherit teams while creating an assignment team' do
   create(:assignment)
   create(:assignment_node)
   create(:assignment_team)
   login_as("instructor6")
   visit '/teams/list?id=1&type=Assignment'
   click_link 'Create Team'
   expect(page).to have_content('Inherit Teams From Course')
 end

To test that while creating a course team, the inherit teams section should not be displayed, we do same steps as previous case but finally we test that the page does NOT contain 'Inherit Teams from Course'


 it 'should not display inherit teams while creating a course team' do
   create(:course)
   create(:course_node)
   create(:course_team)
   login_as("instructor6")
   visit '/teams/list?id=1&type=Course'
   click_link 'Create Team'
   expect(page).to have_no_content('Inherit Teams From Course')
 end

Similarly, for creating a team for an assignment without a course, we test that the page does not contain 'Inherit Teams From Course'. For this particular test case, we added a new factory object for that defined an assignment with course set to nil.


 it 'should not display inherit teams while creating team for an assignment without a course' do
   create(:assignment_without_course)
   create(:assignment_without_course_node)
   create(:assignment_without_course_team)
   login_as("instructor6")    
   visit '/teams/list?id=1&type=Assignment'
   click_link 'Create Team'
   expect(page).to have_no_content('Inherit Teams From Course')
 end

Testing Create Method

This test checks that after creating an assignment team the count in the teams table increase by 1.

   describe "POST #create" do
   context "with an assignment team" do
     it "increases count by 1" do	
       expect{create :assignment_team, assignment: @assignment}.to change(Team,:count).by(1)
     end
    it "redirects to the list page" do
     end
   end
  end

The same test for a course team as well

   context "with a course team" do
     it "increases the count by 1" do
       expect{create :course_team, course: @course}.to change(Team,:count).by(1)
     end
   end

Testing Delete Method

The delete method should work for deleting both assignment and course teams. We check this functionality by deleting the respective team and then check if the count of Teams goes down by 1.

  context "with an assignment team " do
     it "deletes an assignment team" do
       @assignment = create(:assignment)
       @a_team = create(:assignment_team)
       expect{ @a_team.delete }.to change(Team, :count).by(-1)
     end
   end


   context "with a course team " do
     it "deletes a course team" do
       @course = create(:course)
       @c_team = create(:course_team)
       expect{ @c_team.delete }.to change(Team, :count).by(-1)
     end
   end

Testing List Method

The list method lists all the team nodes. This test that we have written, checks whether the instructor is able to view the team nodes in the list view.

describe 'List Team' do

  it 'should list all team nodes' do
    create(:assignment)
    create(:assignment_node)
    assignment_team = create(:assignment_team)
   team_user = create(:team_user)
    login_as("instructor6")
    visit '/teams/list?id=1&type=Assignment'
    page.all('#theTable tr').each do |tr|
      expect(tr).to have_content?(assignment_team.name)
    end
   end
 end

References

  • Github link to the project.
  • Link to the project description.