CSC/ECE 517 Fall 2018/E1853 Write unit tests for menu.rb

From Expertiza_Wiki
Revision as of 01:39, 3 November 2018 by Kmkangut (talk | contribs) (→‎Design)
Jump to navigation Jump to search

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 MenuItems based on the current user's Role. Before this project, there were no unit tests for menu.rb. This project seeks 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 spec file can be run by calling the following 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 an easy look at the work we did.

Design

In total, we wrote 29 tests that covered all the functions in both Menu and its internal Node class. We created 6 MenuItems for testing using the given factories as well as 1 Role used to test the Menu constructor. We stubbed the MenuItem method, items_for_permissions, to return an array of the 6 test items that we created. Other pieces of the code were stubbed because these pieces should be tested in other model specs and not Menu spec. The double 'temp' is created, which acts as a stand-in for some other class objects which interact with Menu, in order to only test the functionalities of Menu.

Each method in menu.rb has at least two tests checking both a known success case and an edge case or potential failure. As an example, a detailed look at the tests for Menu#initialize is shown below.

  context "when role is nil" do
    it "creates a new menu" do
      menu = Menu.new
      expect(menu.instance_of?(Menu))
    end
  end

This first test revealed an error in the existing menu.rb code in the line shown below.

  items = MenuItem.items_for_permissions(role.try(:cache)[:credentials].try(:permission_ids))

The below line shows the change that we made. This allowed the code to be more robust and prevent NoMethodError 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).try(:[], :credentials).try(:permission_ids))

The second test covers the main use case of Menu. It is supplied with a role and assembles a menu. Only a single role is tested because further testing of roles and menus should instead be handled in integration tests.

    context "when a role is passed as an argument" do
      it "creates a new menu" do
        admin_role = build(:role_of_administrator, id: 3, name: "Administrator", description: '', parent_id: nil, default_page_id: nil)
        menu = Menu.new(admin_role)
        expect(menu.instance_of?(Menu))
      end
    end

The third test checks that when the menu is created with items, these items are put into nodes, arranged appropriately and contained within the menu. In this case the children of root will contain a single item, the node with id 1, because this node has a nil parent. The other nodes all have non-nil parents.

    context "when menu has items" do
      it "creates a new menu with items" do
        menu = Menu.new
        expect(menu.root.children.length).to eq(1)
      end
    end

The final test checks a menu without any nodes. While this case is unlikely in actual use, it is important that it can be handled without throwing any errors. When a menu without any items is created, the root will never have anything added to its children array so this array will be nil.

    context "when menu has not items" do
      it "creates a new menu without items" do
        allow(MenuItem).to receive(:items_for_permissions).with(anything).and_return([])
        menu = Menu.new
        expect(menu.root.children).to be_nil
      end
    end

These 4 tests alone provide nearly 85% coverage of menu.rb because of how much they rely on a variety of other methods within the class.

Many of the functions in menu.rb return a Menu::Node. The simplest, Menu#get_item, takes a Node id and returns the corresponding Node object. All following functions use this to test that the correct Node is returned. However to prevent circular logic, the Menu#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.

The main repository can be found here

The forked git repository for this project can be found here

List of all tests

Below is a list of the descriptions of all of the tests we wrote