Mocking

We recommend to use Sinon.JS as a library to create spies, stubs, mocks, and other test helpers.

This framework is used by the map.apps developers for evaluation purpose and is not heavily used.

Installation

To integrate Sinon.JS into a map.apps Maven project add the following Maven dependency:

<dependency>
    <groupId>de.conterra.jsrt</groupId>
    <artifactId>ct-jsrt-test-sinon</artifactId>
</dependency>

To test, which version of Sinon.JS is actually deployed, open http://localhost:8080/resources/jsregistry/root/sinon in a browser.

Usage

This is a sample of a intern test using Sinon.JS to spy a method call. Sinon.JS can be used with any test framework.

tests/hello.js
define([
    "intern!object",
    "intern/chai!assert",
    "module",
    "sinon", // sinon api
    "../hello"
], function (registerSuite, assert, md, sinon, hello) {
    registerSuite({
        name: md.id,

        "test that sayHello is called with message 'sinon'": function () {

            // create a spy watching the method "sayHello"
            var spy = sinon.spy(hello, "sayHello");

            // trigger a "hello"
            hello.sayHello("sinon");

            // check if the spy is called (obviously it is)
            assert(spy.calledWith("sinon"), "expected that the spy tracks the parameter");
        }
    });
});