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

From Expertiza_Wiki
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.

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.

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.

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.

Refactoring

Refactoring 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

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

Instructions for testing Rspecs

1. git clone https://github.com/bhaskarsinha1311/expertiza.git

2. Change to the expertiza directory and then perform "bundle install" and rake db:migrate.

3. Start the rails server

4. In a new terminal and in the expertiza directory, perform the following commands:

   i.  rspec spec/controllers/teams_controller_spec.rb
   ii. rspec spec/features/inherit_teams_display_spec.rb
   iii. rspec spec/features/list_teams_spec.rb

The results to the above commands can be found here.

Testing Inherit Method

To test that while creating an assignment team, the inherit teams section should be displayed, we log in 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'. To do this test manually, user need lo log in as an Instructor and navigate to assignments page for any course. There the user can click on 'Create Teams' icon and will be redirected to the Create teams page for an assignment, there the user can check for the 'inherit teams' button in the view, ideally, it should be there.

 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 in the previous case but finally, we test that the page does NOT contain 'Inherit Teams from Course'. To do this test manually, user need lo log in as an Instructor and navigate to courses page. There the user can click on 'Create Teams' icon and will be redirected to 'create team' page, ideally, the 'Inherit Teams' button should not be displayed.


 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 defined as an assignment with the course set to nil. This can not be tested manually because assignment cannot be created without a course parent.


 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. This test cannot be performed manually as it is testing the count of a database table. But, the user can check this functionality online by logging in as an instructor, going to the assignment page of any course and clicking on 'create teams' icon. Following up with entering the required details and clicking on 'create team'.

   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 an be applied to create 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. The user can check this test manually by logging in as an instructor navigating to the page of any team for an assignment and clicking on 'Delete Team' icon.

  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

Same Delete test has been applied to the course team The manual check can also be performed in a similar way by logging in as an instructor navigating to the page of any team for an assignment and clicking on 'Delete Team' icon.


   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. To do this test manually user can log into the system as an instructor and click on any course, the course teams will be displayed. Also, the user can click on any team node to view information about that team.

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