CSC/ECE 517 Fall 2016/E1674.Refactor leaderboard.rb and write unit tests

From Expertiza_Wiki
Revision as of 19:25, 28 October 2016 by Ywei8 (talk | contribs) (→‎Refactor)
Jump to navigation Jump to search

This page is giving a description of the changed made for the Expertiza based OSS project.

Expertiza Background

Expertiza is a web application where students can submit and peer-review learning objects (articles, code, web sites, etc). Instructor adds and edits assignment to Expertiza. And students can be assigned in team based on their selection of the topics. The Expertiza project is supported by the National Science Foundation.

Problem Statement

The task of the project is to refactor leaderboard.rb and write unit tests for it. As the names of the methods are not in a standard style, so we used snake_case for method names. There are also some useless assignments to several variables. The code climate rating of leaderboard.rb is D, which is needed to be improved.

Modified File

  • leaderboard.rb
  • create a RSpec file in /spec/models/ foler which contains unit test for leaderboard.rb


Thoughts

What we need to do is to deal with the problems listed by the code climate. And we also need to write a new test file to test the functionality of leaderboard.rb. However, the leaderboard itself has some problems and it will report some problems when we want to run it. So we need to know which method in this file is broken first and just test others instead.

Tools

As for this project, we used the code climate plugin to test if our code is good enough. This plugin will obviously help us with the refactor part because it can show which part of the original codes have some problems. We also searched through the internet and get to know how to write the Rspec files.

Refactor

We will list each kind of problems we solved below.

Use snake_case for method names.

As we all know the snake_case means we need to name the methods and variables in this way. For example, we changed this method. def self.getIndependantAssignments(user_id)

   assignmentIds = AssignmentParticipant.where(user_id: user_id).pluck(:parent_id)
   noCourseAssignments = Assignment.where(id: assignmentIds, course_id: nil)
 end

Useless assignment to variable - `noCourseAssignments`.

Useless assignment to variable - `assignmentList`.

Prefer `each` over `for`.

Prefer `each` over `for`.

Avoid more than 3 levels of block nesting.

`end` at 148, 1 is not aligned with `def` at 129, 2.

Rspec Test

Addtional