CSC/ECE 517 Fall 2016/E1634. Refactor and write unit test of due date.rb and deadline helper.rb

From Expertiza_Wiki
Revision as of 23:26, 28 October 2016 by Nthanik (talk | contribs)
Jump to navigation Jump to search

Introduction

Expertiza

Expertiza<ref></ref> is an open source project for school assignment management for instructors and students based on the Ruby on Rails<ref></ref> framework. Expertiza allows the instructor to create new assignments and customise 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 word documents. Expertiza provides a dashboard for all the assignments corresponding to a course and provides absolute control to the Instructors and Teaching Assistants. In addition to assignments, it encompasses peer reviews wherein participants are allowed to provide feedback anonymously about each other's work thereby providing scope for the better outcome. The due_date.rb file is responsible for informing the users about the deadline for submission of the each assignment. Due dates in Expertiza have their association with many other components like assignments, reviews etc.

Code Climate

Code Climate<ref></ref> is a tool that runs static analysis on a GitHub project and outputs many details like test coverage, complexity, duplication, security, style, and more. There is a Google Chrome extension to integrate the Code Climate results generated directly into GitHub which is visible when browsing the repository on the browser. It allows us to see issues displayed directly inside GitHub's UI, to review which lines are covered in diffs and files, and add repositories and open tickets without changing your workflow.

Tasks Identified

To install Code climate Chrome Extension that highlights the duplicated code.

To refactor the following two files:

  • Due_date.rb
    • Ternary operators must not be nested. Prefer `if` or `else` constructs instead.
    • Useless assignment to variable - `sorted_deadlines`.
    • Prefer `each` over `for`.
    • Use `find_by` instead of `where.first`.
    • Correct the use of Time function
  • Deadline_helper.rb
    • Do not use `Time.now` without zone. Use one of `Time.zone.now`, `Time.current`, `Time.now.in_time_zone`, `Time.now.utc`, `Time.now.getlocal`, `Time.now.iso8601`, `Time.now.jisx0301`, `Time.now.rfc3339`, `Time.now.to_i`, `Time.now.to_f` instead.
    • Trailing whitespace detected.
    • Extra empty line detected at module body end.
  • Create respective RSpec<ref></ref>files in /spec/models/ and /spec/helper folder and write unit tests for each method in due_date.rb and deadline_helper.rb.

Current Implementation

DueDate is a Model class to manage the deadlines of an assignment. It has methods for setting due dates for an assignment, copying due dates from one assignment to a new assignment etc. DeadlineHelper provides helper functions that help DueDate perform certain tasks. The assignment focuses on refactoring some of the methods based on warnings received from Code Climate's static analysis and modifying the language to make it more Ruby-friendly. The assignment also involves writing unit test cases for due_date.rb and deadline_helper.rb in order to increase test coverage.

The goal of this project is to attempt to make this part of the application easier to read and write unit test cases that the application must pass.

Changed Implementation

Changes implemented involves refactoring the code and making it more understandable by adding comments in the code.

The modified files are

  • Due_date.rb (path: /app/models)
  • Deadline_helper.rb (path: /app/helpers)

Testing files

  • Due_date_spec.rb (path: /spec/models)
  • Deadline_helper_spec.rb (path: /spec/helpers)

Due_date.rb

  • Converted for..in loop to object.each in order to follow better Ruby syntax.
  • Unnecessary assignment to sorted_deadlines removed.
  • Nested ternary operators have been changed to if..else in order to make it more readable.

  • Changed where(...).first to find_by(...) which is the newer recommended syntax.
  • Corrected the Time.now functions by adding the correct zones to them such as Time.zone.now.
  • Removed trailing whitespaces.

Deadline_helper.rb

  • Time functions were changed to functions with zones
  • Extra line removed