TestServiceProvider

The TestServiceProvider is meant to be a substitute for the traditional context and application that T3 Modules/Behaviors/Services usually have access to. The object allows for easy stubbing of T3 services and can enable unit tests to use the real service, if necessary.

Usage

Parameter Type Description
serviceStubs Object A map of service names to stubs.
allowedServices string[] List of real services to use. You must include the service in your test script.

Service Stub Example

var context = new Box.TestServiceProvider({
    logger: {
        warn: function() {}
    }
});

var module = Box.Application.getModuleForTest('foo', context);

Real Service Usage Example

var context = new Box.TestServiceProvider({}, ['logger'] );

var module = Box.Application.getModuleForTest('bar', context);

getService

Description

Retrieves either a service stub or the real service.

Returns

A service object or throws an error if service does not exist.

Example

var context = new Box.TestServiceProvider({
    logger: {
        warn: function() {}
    }
});

var loggerService = context.getService('logger');

hasService

Description

Checks if a service stub or the real service exists

Returns

True if the service exists. False otherwise.

Example

var context = new Box.TestServiceProvider({
    logger: {
        warn: function() {}
    }
});

// True
context.hasService('logger');

// False
context.hasService('notifications');

getGlobal

Description

Returns a global variable.

Returns

A global variable or null.

Example

var context = new Box.TestServiceProvider({});

// Returns the window object
context.getGlobal('window');