$UnitTest class
$UnitTest
class will allow you to import the class you want to test in a safe context. It will expose multiple methods to allow you to retrieve class depending on their types (Class, Model, Trait).
getClass
This is probably the most used one. It will allow you to retrieve class that inherits from $Class
including Service, View, Screen… It will have the equivalent method destroyClass
to destroy your class.
describe("MyClass", function () {
let MyClass, myClass;
/**
* Load the Classes needed for the tests, initialize global variables
*/
before(function () {
return $UnitTest.getClass("@MyClass").then(function (loadedClasses) {
MyClass = loadedClasses.class;
});
});
/**
* Destroy the Class(es), cleanup
*/
after(function () {
return $UnitTest.destroyClass(MyClass);
});
/**
* Before each test, create a fresh instance of MyClass
*/
beforeEach(function () {
myClass = new MyClass();
});
it("should have created an instance of 'MyClass'", function () {
expect(myClass).to.exist;
expect(myClass).to.be.an.instanceof(MyClass);
});
});
getModel
It will allow you to retrieve class that inherits from $Model
including BusinessModel, ImageModel… It will have the equivalent method destroyModel
to destroy your class.
describe("$MyModel", function () {
let MyModel, myModel;
/**
* Load the Model and classes needed for the tests, initialize global variables
*/
before(function () {
return $UnitTest.getModel("@MyModel", {dependencies: []}).then(function (loadedClasses) {
MyModel = loadedClasses.model;
});
});
/**
* Destroy the Model(s), cleanup
*/
after(function () {
return $UnitTest.destroyModel(MyModel);
});
/**
* Before each test, create a fresh instance of MyModel
*/
beforeEach(function () {
myModel = new MyModel();
});
it("should have created an instance of 'MyModel'", function () {
expect(myModel).to.exist;
expect(myModel).to.be.an.instanceof(MyModel);
});
});
getTrait
It will allow you to retrieve class that inherits from $Trait
. It will have the equivalent method destroyTrait
to destroy your class.
describe("MyTrait", function () {
let MyTrait, myTrait;
/**
* Load the Trait needed for the tests, initialize global variables
*/
before(function () {
return $UnitTest.getTrait("@MyTrait").then(function (loadedClasses) {
MyTrait = loadedClasses.trait;
});
});
/**
* Destroy the Trait, cleanup
*/
after(function () {
$UnitTest.destroyClass(MyTrait);
});
/**
* Before each test, create a fresh instance of MyTrait
*/
beforeEach(function () {
myTrait = MyTrait.create();
});
it("should have created an instance of 'MyTrait'", function () {
expect(myTrait).to.exist;
});
});
Options
To each of the above function, you will be able to send an object as a second parameter that could contain following keywords:
- dependencies: an array of dependencies that will be needed
describe('MyClass', function () {
let myClassInstance, MyClass, Dep;
/**
* Load the Classes needed for the tests, initialize global variables
*/
before(function () {Ε
return $UnitTest.getClass("@MyClass", {
dependencies: ["@Dep"]
}).then(function (loadedClasses) {
MyClass = loadedClasses.class;
Dep = loadedClasses.dependencies[0];
});
/**
* Destroy the Class(es), cleanup
*/
after(function () {
return $UnitTest.destroyClass(MyClass);
});
/**
* Before each test, create a fresh instance of MyClass
*/
beforeEach(function () {
myClass = new MyClass();
});
});
});
- singletons: an array of singletons of which you may change state. They will be reset after your test automatically in the
destroy
.
describe('MyClass', function () {
let myClassInstance, MyClass, MyService;
before(function () {
return $UnitTest.getClass("@MyClass", {
dependencies: ["@MyService"],
singletons: ["@MyService"]
}).then(function (loadedClasses) {
MyClass = loadedClasses.class;
MyService = loadedClasses.dependencies[0];
});
beforeEach(function () {
MyService.$$instance = {
myFunction: sinon.spy()
}
myClassInstance = new MyClass();
});
after(function () {
return $UnitTest.destroyClass(MyClass);
});
it("should call methodToSpy", function () {
MyService.myFunction();
expect(MyService.myFunction).to.have.beenCalledOnce;
});
});
});
- spies: an array of method names (events or functions) to spy
describe('MyClass', function () {
let myClassInstance, MyClass;
before(function () {
return $UnitTest.getClass("@MyClass", {
spies: ["methodToSpy"]
}).then(function (loadedClasses) {
MyClass = loadedClasses.class;
});
beforeEach(function () {
// Reset spies
MyClass.spies.reset();
myClassInstance = new MyClass();
});
after(function () {
return $UnitTest.destroyClass(MyClass);
});
it("should call methodToSpy", function () {
myClassInstance.methodToSpy();
expect(myClassInstance.methodToSpy).to.have.beenCalledOnce;
});
});
});
- prototype: map of class members to override into the copied test class
describe('MyClass', function () {
let myClassInstance, MyClass;
/**
* Load the Classes needed for the tests, initialize global variables
*/
before(function () {
return $UnitTest.getClass("@MyClass", {
prototype: {
myFunction: function () {
return Promise.resolve(CURRENT_PROGRAMS);
}
}
}).then(function (loadedClasses) {
MyClass = loadedClasses.class;
});
/**
* Destroy the Class(es), cleanup
*/
after(function () {
return $UnitTest.destroyClass(MyClass);
});
/**
* Before each test, create a fresh instance of MyClass
*/
beforeEach(function () {
myClass = new MyClass();
});
it("should call myFunction", function () {
myClass.myFunction();
expect(myClass.myFunction).to.have.beenCalledOnce;
});
});
});