CSC/ECE 517 Spring 2025 - E2506. Implement testing for new Bookmarks Controller

From Expertiza_Wiki
Jump to navigation Jump to search

E2516: Reimplement testing for new Bookmarks Controller

Introduction

Expertiza is an open-source Ruby on Rails application designed to help teachers manage classrooms. It is maintained by the teaching staff at NC State, along with the students who use it. The application includes various models, such as assignments, questions, and users. Expertiza, students can organize into different teams to work on different assignments and projects. Also, students have the capability to peer review as a way for students to evaluate each other's work. Expertiza allows submissions from a variety of document types, such as PDFs, wiki pages and URLs.

Project Overview

This project focuses on enhancing the BookmarksController in the Expertiza platform by adding comprehensive test coverage, generating API documentation, and improving code quality and maintainability. The main objectives are to:

  • Develop Tests: Achieve at least 90% test coverage for CRUD operations, bookmark ratings, and score calculations, ensuring edge cases and error handling are properly validated.
  • Generate API Documentation: Use Swagger UI to create clear and accurate API documentation for the BookmarksController.
  • Refactor Code: Optimize the code using Rubocop and Code Climate, remove unused functionality, and clarify ambiguous methods to improve maintainability and performance.
  • Authorization & Error Handling: Ensure proper authorization rules are enforced, and handle errors with informative messages.

Files Modified

The following files have been modified to achieve the mentioned functionalities:

  • Role_test.rb


role_test.rb

The changes made were:

  • Test 1: test_bookmark_creation_validity

This test case verifies the validity of a bookmark creation process. It checks that a bookmark is successfully created when valid parameters are provided, ensuring the bookmark is saved to the database. Additionally, it validates that the bookmark creation fails if essential attributes (e.g., title or URL) are missing, returning the appropriate error message to guide the user.

  • Test 2: test_bookmark_rating

This test case ensures the correct functionality of the bookmark rating feature. It verifies that a bookmark can receive a rating, and that the average rating is updated correctly when a user rates it. Additionally, it checks that the system properly handles invalid ratings, such as out-of-range values or duplicate ratings from the same user.

  • Test 3: test_bookmark_deletion

This test case tests the functionality of bookmark deletion. It ensures that a bookmark can be successfully deleted when a user requests it, and that it is no longer present in the database afterward. Additionally, it checks that attempting to delete a non-existent bookmark returns the appropriate error message.

 test 'validates name length' do
   role = Role.new(name: 'A' * 51) # assuming max length is 50
   assert_not role.valid?
   assert_equal ['is too long (maximum is 50 characters)'], role.errors[:name]
   role.name = 'A' * 2 # assuming min length is 3
   assert_not role.valid?
   assert_equal ['is too short (minimum is 3 characters)'], role.errors[:name]
 end
 # Test for role parent-child relationships
 test 'parent-child relationship' do
   parent_role = Role.create!(name: 'Parent Role')
   child_role = Role.create!(name: 'Child Role', parent: parent_role)
   assert_equal parent_role, child_role.parent
   assert_includes parent_role.subordinate_roles, child_role
 end
 # Test for role deletion behavior
 test 'role deletion cascade' do
   parent_role = Role.create!(name: 'Parent Role')
   child_role = Role.create!(name: 'Child Role', parent: parent_role)
   parent_role.destroy
   assert_raises(ActiveRecord::RecordNotFound) { child_role.reload }
 end



Mentor

Team Members