Skip to content
This repository was archived by the owner on Apr 6, 2020. It is now read-only.

6. Automatic testing

s.mombuleau edited this page Dec 4, 2018 · 2 revisions

Unit test with mocka

Testing your IA is very important. In order to automatically test the modules, the IA project bundles the Mocha framework, a complete test suite.

Running the tests

One command line:

$ tests

It will run the mocha test suite.

Implementing the tests

If a module contains a test/ folder, it will automatically be tested by the mocha runner.

Examples

Testing for expected result using assert
describe('Calculator', function() {
it('Respond with matching records (3x2 = 6)', function(done) {
    let resultPromise = Calculator.getData(['3*2'], '', "en_gb", i18n);
    resultPromise.then((result) => {
        assert.equal(result, 6);
        done();
    }).catch((error) => {
        done(error);
    })
});
Testing for error handling

Note: the done function without parameter corresponds to a success and with a parameter is an error

it('Handling error', function(done) {
    let resultPromise = Calculator.getData(['3****2'], '', "en_gb", i18n);
    resultPromise.then((result) => {
        done('Error should be handled');
    }).catch((error) => {
        if (error === "Your formula isn't valid") {
            done();
        } else {
            done(error);
        }
    })
});

How to mock data

Proxyquire dynamically injects your mock inside the module to test. You can inject a mock into a module by requiring the module using proxyquire and passing along the mock to use.

Here's an example for an hypothetical geo_solver which uses an elasticsearch config:

const geoSolver = proxyquire('../geo_solver', {'elasticsearch':elasticMock});

Links related