CSC/ECE 517 Fall 2017/E1752 Refactor assignments controller: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
(update refactor)
(add constant part)
Line 19: Line 19:
RSpec is a 'Domain Specific Language' (DSL) testing tool written in Ruby to test Ruby code. It is a behavior-driven development (BDD) framework which is extensively used in the production applications. The basic idea behind this concept is that of Test Driven Development(TDD) where the tests are written first and the development is based on writing just enough code that will fulfill those tests followed by refactoring.  
RSpec is a 'Domain Specific Language' (DSL) testing tool written in Ruby to test Ruby code. It is a behavior-driven development (BDD) framework which is extensively used in the production applications. The basic idea behind this concept is that of Test Driven Development(TDD) where the tests are written first and the development is based on writing just enough code that will fulfill those tests followed by refactoring.  


The Expertiza project makes extensive use of RSpec to test the various components of the application.
The Expertiza project makes extensive use of RSpec to test the various components of the application. Tests of our tasks are in the code file: spec/controllers/assignments_controller_spec.rb


===Testing actions perfomed===
===Testing actions perfomed===
Line 29: Line 29:


===Refactoring actions perfomed===
===Refactoring actions perfomed===
* Replace magic numbers in deadline_type_id by constants with reasonable names;
* Formatting the code for readability and convention;
* Formatting the code for readability and convention;
* Split large chunks of code in to smaller manageable chunks;
* Split large chunks of code in to smaller manageable chunks;
Line 63: Line 64:
       end
       end


It is obvious that after refactoring, this part is simpler and easier to maintain. After each refactoring procedure, we perform the testing to ensure the expected functionality in Rspec. For more cases, please check our GitHub project repo.
It is obvious that after refactoring, this part is simpler and easier to maintain. After each refactoring procedure, we perform the testing to ensure the expected functionality in Rspec. For more cases, please check our GitHub project repo. The files we make change on are: app/controllers/assignments_controller.rb and app/helpers/deadline_helper.rb


==Additional Links==
==Additional Links==

Revision as of 01:17, 28 October 2017

Introduction

Expertiza is an open source webapp built on Ruby on Rails stack. It provides a platform to students with various features like peer-reviewing projects, submitting work, form teams, viewing grades etc. The project is being built and maintained by students and faculty at NCSU.

About Assignments Controller

The file assignments_controller.rb handles the logic behind Expertiza assignments and enables the creation and managements of assignments by instructional staff. Besides regular CRUD operations, it also integrates multiple parts of the expertiza components, such as users of different authorizations and priorities, corresponding courses associated with the assignment, due date and so on. Therefore, further unit and integration tests are required to guarantee that these operations work as expected. And also refactoring on the code in this controller is needed to make it more readable and reduce the complexity at the same time. Our tasks are based on the motivations above.

About Software Testing

Software testing is a process of examining a program or application with the intent of finding the software bugs. In software developemnt, it is usually defined as the process of validating and verifying that a software program or application or product meets the specified business and technical requirements.

Additionaly, software is tested to:

  • Detect and rectify and errors made during the development phase,
  • Ensure customer satisfaction in the application,
  • Ensure the quality of the software product and
  • Optimize the performance of the system.

Our project is guided by the Test-First Development principle. We write passing and failing tests for use cases of the assignment controller, and then turn to refactor the code. During the project, we implement the tests and refactor in Agile methodology with our mentor Zhewei through weekly delivery.

RSpec Testing

RSpec is a 'Domain Specific Language' (DSL) testing tool written in Ruby to test Ruby code. It is a behavior-driven development (BDD) framework which is extensively used in the production applications. The basic idea behind this concept is that of Test Driven Development(TDD) where the tests are written first and the development is based on writing just enough code that will fulfill those tests followed by refactoring.

The Expertiza project makes extensive use of RSpec to test the various components of the application. Tests of our tasks are in the code file: spec/controllers/assignments_controller_spec.rb

Testing actions perfomed

  • Creating new tests for each method,
  • Testing the each method and making sure the tests pass.

About Refactoring

Refactoring of computer software or code is the process of restructuring existing software code without changing its external behavior or performance of the software application. Refactoring improves nonfunctional attributes of the software. Refactoring aids in code readability and keeps the code well maintained and easy to understand.

Refactoring actions perfomed

  • Replace magic numbers in deadline_type_id by constants with reasonable names;
  • Formatting the code for readability and convention;
  • Split large chunks of code in to smaller manageable chunks;
  • Extract simpler and specific methods out of blocks of codes to remove code duplications using DRY principle;
  • Rename existing methods with meaningful names for better readability without change on external functionality.

Example: code block split and method extraction

Original

     @due_date_nameurl_notempty = is_due_date_nameurl_notempty(dd)
     @due_date_nameurl_notempty_checkbox = @due_date_nameurl_notempty
     @metareview_allowed = is_meta_review_allowed?(dd)
     @drop_topic_allowed = is_drop_topic_allowed?(dd)
     @signup_allowed = is_signup_allowed?(dd)
     @team_formation_allowed = is_team_formation_allowed?(dd)
     if dd.due_at.present?
       dd.due_at = dd.due_at.to_s.in_time_zone(current_user.timezonepref)
     end
     if  @due_date_nameurl_notempty && @due_date_nameurl_notempty_checkbox &&
         (@metareview_allowed || @drop_topic_allowed || @signup_allowed || @team_formation_allowed)
       break
     end

After refactoring

     due_date_nameurl_notempty_check(dd)
     due_date_present(dd)
     if due_date_validation
       break
     end

It is obvious that after refactoring, this part is simpler and easier to maintain. After each refactoring procedure, we perform the testing to ensure the expected functionality in Rspec. For more cases, please check our GitHub project repo. The files we make change on are: app/controllers/assignments_controller.rb and app/helpers/deadline_helper.rb

Additional Links

Github project repo
Project Video Demo

References

Rspec Documentation
Better Specs
Expertiza Wiki
Expertiza NCSU

Team

Shijie Li
Amey Deshmukh
Philip Musyoki