CSC/ECE 517 Spring 2020 - Project E2011. Refactor assignment creation spec.rb
E2011. Refactoring assignment_creation_spec.rb
This page provides a description of the Expertiza based OSS project.
Introduction
Expertiza is an open source project based on Ruby on Rails framework. Expertiza allows the instructor to create new assignments and customize new or existing assignments. It also allows the instructor to create a list of topics the students can sign up for. Students can form teams in Expertiza to work on various projects and assignments. Students can also peer review other students' submissions. Expertiza supports submission across various document types, including the URLs and wiki pages.
Problem Statement
The two major tasks for this project were the following:
- Refactor existing test cases to follow good coding practices. This includes variable names, following the Don't Repeat Yourself (DRY) principle, and more.
- Check coverage of assignment_creation_spec.rb and improve upon it through additional testing.
Current Implementation
The assignment_creation_spec file contains test cases that ensure proper functionality of an instructor's workflow in regards to the creation of assignments. An instructor, when logged into Expertiza, should be able to create new assignments where each assignment has its own assigned parameters, rubrics, review strategies and due dates. At the start of this project, all logic and handling of testing this instructor workflow was handled within a single file. This single-file implementation leads to problems with maintenance, and such problems within this implementation can be abstracted into one of these forms:
- File is too long: This file exceeds 250 lines of code, and should not.
- Block has too many lines: Within the file, the code used to handle a certain operation is implemented with too many subroutines within one block.
- Similar/Exact block(s) of code found: Across the file, there were duplicate/near-duplicate blocks of code being used.
- General syntax issues: This could include unnecessary whitespace, use of deprecated classes or things of the like.
Refactoring Process
The first refactor that our team did was to extract methods from what otherwise would be duplicate code and large block sizes. The previous team had started this process, but we were able to follow it to completion. We also then moved all of these extracted methods into the helper class assignment_creation_helper.rb. Here is an example of extracted methods that the previous team had created:
def get_questionnaire(finder_var = nil) if finder_var.nil? AssignmentQuestionnaire.find_by(assignment_id: @assignment.id) else AssignmentQuestionnaire.where(assignment_id: @assignment.id).where(questionnaire_id: get_selected_id(finder_var)) end end def get_selected_id(finder_var) if finder_var == "ReviewQuestionnaire2" ReviewQuestionnaire.find_by(name: finder_var).id elsif finder_var == "AuthorFeedbackQuestionnaire2" AuthorFeedbackQuestionnaire.find_by(name: finder_var).id elsif finder_var == "TeammateReviewQuestionnaire2" TeammateReviewQuestionnaire.find_by(name: finder_var).id end end def fill_assignment_form fill_in 'assignment_form_assignment_name', with: 'edit assignment for test' select('Course 2', from: 'assignment_form_assignment_course_id') fill_in 'assignment_form_assignment_directory_path', with: 'testDirectory1' fill_in 'assignment_form_assignment_spec_location', with: 'testLocation1' end def assignment_creation_setup(privacy,name) login_as("instructor6") new_assignment_url = "/assignments/new?private=#{privacy}" visit new_assignment_url fill_in 'assignment_form_assignment_name', with: name select('Course 2', from: 'assignment_form_assignment_course_id') fill_in 'assignment_form_assignment_directory_path', with: 'testDirectory' end
The following is an example of how we were able to reduce block size and duplicate code with further extraction of methods into assignment_creation_helper.rb:
Another major refactor we did was to break up the one large 'describe' block in assignment_creation_spec, which contained all of the tests for that controller. Since assignment creation has many different pages, we created a separate file for testing each page or responsibility.
Beyond the creation of the helper file, assignment_creation_spec.rb was broken up into the following files:
- assignment_creation_dates_spec.rb
- assignment_creation_deadlines_spec.rb
- assignment_creation_general_tab_spec.rb
- assignment_creation_page_spec.rb
- assignment_creation_participants_spec.rb
- assignment_creation_review_strategy_spec.rb
- assignment_creation_rubrics_spec.rb
- assignment_creation_topics_spec.rb
In the relocation of assignment_creation_spec's functionality and creation of new constituent files, additional refactoring was done to ensure that trailing whitespace was deleted, files ended with a final newline character, and other unnecessary characters were removed. All of this was to remove any other problems that code climate detected.
Additional Code Climate Issues
There were a few things that code climate detected that we decided did not need to be refactored. The first was a complaint about blocks being too large, even after we divided the one large 'describe' statement into many smaller ones. This is because, in rspec, each test has its own 'it' block, but all of those must be grouped in a larger 'describe' block. I suggest that this be discussed with the teams who may refactor the other test files or have code climate to be configured to instead look just at the 'it' block size.
This is an diagram on how otherwise good ruby code may be flagged by code climate:
'describe' block 'it' block (23 lines of code) - Code climate: √ 'it' block (15 lines of code) - Code climate: √ 'it' block (11 lines of code) - Code climate: √ end - Code climate: X (23+15+11 = 49 lines)
Additionally, code climate was suggesting to change DateTime to Date or Time. As assignments are due not only on a certain day but at a specific time and that this would require a large refactoring of the entire Expertiza system for tracking submission times, we decided it was beyond the scope of this refactor.
Testing Plan
As part of the refactoring, we fixed a few broken tests in the original assignments_controller_spec.rb and created additional ones to increase coverage. We used TDD to see which tests that were created were breaking and used that to drive our implementation of the controller.
Here is the extensive list of test cases handled
1. assignments_controller_spec.rb
-allows edit when an admin
-allows edit when an instructor
-does not allow edit when TA of course but not TA of current assignment
-allows new when admin/instructor/ta
-does not allow new when student
-#new creates a new AssignmentForm object and renders assignment#new page
-when assignment_form is saved successfully, it redirects to assignment#edit page
-when assignment_form is not saved successfully, it renders assignment#new page
-when assignment has staggered deadlines, it shows a flash message and renders edit page
-during update when params does not have key :assignment_form, it shows a note flash message and redirects to tree_display#index page
-when assignment is not saved successfully, it shoes an error flash message and redirects to assignments#edit page
-when the timezone preference of current user is nil and assignment form updates attributes successfully, it shows an error message and redirects to assignments#edit page
-when the timezone preference of current user is not nil and assignment form updates attributes successfully, it shows an error message and redirects to assignments#edit page
-on action #show, it renders the #show page
-when new assignment id fetches successfully, it 'redirects to assignments#edit page
-when new assignment directory is same as old it should show an error and redirect to assignments#edit page
-when new assignment id does not fetch successfully, it shows an error flash message and redirects to assignments#edit page
-when assignment is deleted successfully, it shows a success flash message and redirects to tree_display#list page
-when assignment is not deleted successfully, it shows an error flash message and redirects to tree_display#list page
-when assignment is removed from course successfully, it removes assignment and redirects to tree_display#list page
-showing list submissions
(the following are the assignment_creation_spec.rb split into files by responsability) 2. assignment_creation_dates_spec.rb
-it is able to create assignment with a new late policy
-it is able to set the deadline for an assignment review
3. assignment_creation_deadlines_spec.rb
-it is able to set the deadline for an assignment review
4. assignment_creation_general_tab.rb
-it should edit assignment available to students
-it should edit quiz number available to students
-it should edit number of members per team
-it should edit review visible to all other reviewers
-it should check if checking calibration shows the tab
5. assignment_creation_page_spec.rb
-it is able to create a public assignment
-it is able to create with teams
-it is able to create with quiz
-it is able to create with staggered deadline
-it is able to create with review visible to all reviewers
-it is able to create public micro-task assignment
-it is able to create calibrated public assignment
-it is able to show tab review strategy
-it is able to show tab due deadlines
-it should set the deadline for an assignment review
-it is able to show tab rubrics
-it is able to show attributes in rubrics
-it should sets attributes for review strategy auto selects
-it should check to find if the assignment can be added to a course
6. assignment_creation_participants_spec.rb
-it should check to see if participants can be added
-it should display newly created assignment 7. assignment_creation_review_strategy_tab.rb
-it should auto selects correct values
-it should set number of reviews by each student
8. assignment_creation_rubrics_strategy_tab.rb
-it should update review questionnaire
-it should update scored question dropdown
-it should update author feedback questionnaire
-it should update teammate review questionnaire
9. assignment_creation_topics_spec.rb
-it can edit topics properties
-it procedes without topics properties
-it can create new topics
-it can delete existing topic
-it hides topics tab when has topics is un-checked
Code of Additional Rspec Tests Created
Looking at the methods that were covered in assignments_controller.rb we decided to create the following additional test cases for assignment creation.
These are additions to the existing assignments_controller_spec.rb.
RSpec Video ScreenCast Demos
Links to RSpec screencasts for new test cases.
Coverage
92.86% for assignments_controller.rb
Overall coverage increased (+28.04%) to 52.334%
Project Mentors
- Dr. Edward Gehringer (efg@ncsu.edu)
- Srujana Rachakonda (srachak@ncsu.edu)
Team Members
- Colin Foley (cmfoley2@ncsu.edu)
- Ayush Khot (akhot@ncsu.edu)
- Christian Morris (cgmorris@ncsu.edu)