Skip to content

Testing using Jasmine

erikvullings edited this page Dec 15, 2014 · 1 revision

Introduction

csCompTests is a separate project that is used to test the csComp library. It:

  1. Uses gulp to copy the generated csComp.js en csComp.d.ts files to the csCompTests/csComp folder.
  2. Is build to a single file, named specifications.js
  3. Loads the jasmine files and other dependencies like angular, csComp.js and specification.js in index.html, and runs the tests in there.

Running the tests

As stated above, just open csCompTests/index.html in your browser, and all tests will be run.

Adding your own tests

This should be quite simple.

  1. Create a new specificationXXX.ts file, where XXX is the name of the model or module you wish to test.
  2. Add test code to it. For an example, see below.
describe('The MCA model ', () => {
    beforeEach(() => {
        this.mca = new Mca.Models.Mca();
        this.mca.criteria.push({ userWeight: 5, criteria: [] });
        this.mca.criteria.push({ userWeight: 3, criteria: [] });
        this.mca.criteria.push({ userWeight: 2, criteria: [] });
    });

    it('should calculate the correct weights based on the user weights.', () => {
        this.mca.calculateWeights();

        expect(this.mca.criteria[0].weight).toBe(0.5);
        expect(this.mca.criteria[1].weight).toBe(0.3);
        expect(this.mca.criteria[2].weight).toBe(0.2);
    });

    it('should assign a color to each criterion after updating.', () => {
        expect(this.mca.criteria[0].color != null).toBe(false);
        expect(this.mca.criteria[1].color != null).toBe(false);
        expect(this.mca.criteria[2].color != null).toBe(false);

        this.mca.update();

        expect(this.mca.criteria[0].color != null).toBe(true);
        expect(this.mca.criteria[1].color != null).toBe(true);
        expect(this.mca.criteria[2].color != null).toBe(true);
    });
});