CSC/ECE 517 Fall 2018/E1853 Write unit tests for menu.rb
This project wrote unit tests for the Menu.rb model in Expertiza.
Project Introduction
The menu model is used to create the top bar menu in expertiza. It does this by obtaining and organizing Menu_Items based on the current users Role. Before this project there were no unit tests for menu.rb. This project seeked to bring the unit test coverage above %90.
Team
Barrett Bryson (bbryson)
Komal Kangutkar (kmkangut)
RSpec File
The final result can be found at expertiza/spec/models/menu_spec.rb. This specific test can be run by calling in the expertiza directory:
rspec -fd ./spec/models/menu_spec.rb
The addition of the -fd argument will write out full descriptions of the tests and provide a easy look at the work we did.
Design
In total we wrote 29 tests that covered all of the functions in both menu and its internal node class. We created a 6 menu items for testing using the given factories as well as 1 role used to test the menu constructor. We stubbed menu item to always return an array of the 6 test items that we created. Other pieces of the code were stubbed because these pieces should be tested by menu item not menu. The double temp is used to only test the functionalities of menu.
Each method in menu.rb has at least two tests checking both a know success case and a edge case or potential failure.
While testing we found in error in the existing menu.rb code in the line shown below. The second line shows the change that we made. This allowed the code to be more robust and prevent noMethodErrors from nilClass. Without this change a menu created with a nil role will throw an error causing the program to fail unnecessarily. This is important because the default value of role is nil and that should not fail.
items = MenuItem.items_for_permissions(role.try(:cache)[:credentials].try(:permission_ids))
items = MenuItem.items_for_permissions(role.try(:cache).try(:[], :credentials).try(:permission_ids))
Many of the functions in menu return a menu node. The simplest, get_item, takes the node id and returns the node object. All following functions use this to test that the correct node is returned. However to prevent circular logic, the get_item test only checks the id of the returned node.
Results
The 29 tests provide 100% coverage of the lines in menu.rb. A video of all tests running can be seen here.
List of all tests
Below is a list of the descriptions of all of the tests we wrote