ModuleCtrlTestCase provides helper methods to instantiate and test module controllers. The class is designed to monitor all events and server connections made by a module.
On top of the assert methods that it inherits from Test Case, it provides the following:
assertLogsClean / assertLogsNotClean Asserts that there is no error in the logs, it is similar to assertLogsEmpty but it also checks for connection errors.
cxLogs, an array of ConnectionSession. These objects contain information on every request that is done through the module controller.
moduleControllerInstance.submitJsonRequest("search", {from : "here"}, callback);
this.cxLogs[0].ioRequest.url; // http://something.here/search
Starting from Aria Templates 1.3.7 ModuleCtrlTestCase is able to automatically create and dispose the instance of Module Controller under test.
By specifying $controller in the test's $prototype an instance of that class is generated before running the test.
Inside the test methods you can access the two variables $moduleCtrl and $moduleCtrlPrivate.
The $moduleCtrl gives a wrapper which offers only the methods and fields exposed via the interface, while $moduleCtrlPrivate gives direct access to the module controller.
The public interface is also registered for asserting its logs, so there's no need to call registerObject.
The test makes sure that objects are created and disposed correctly off-loading some work from the developer.
Aria.classDefinition({
$classpath : "test.ModuleControllerTest",
$extends : "aria.jsunit.ModuleCtrlTestCase",
$dependencies : ["test.mock.RedirectToFile"],
$prototype : {
$controller : "myapp.SimpleController",
setUp : function () {
aria.core.IOFiltersMgr.addFilter("test.mock.RedirectToFile");
},
tearDown : function () {
aria.core.IOFiltersMgr.removeFilter("test.mock.RedirectToFile");
},
testAsyncCallTheServer : function () {
// Call an asynchronous method on the public interface
this.$moduleCtrl.processCommand({
fn : this.after,
scope : this
});
},
after : function () {
this.assertEventFired("responseReceived");
// This checks that there are no response error
this.assertLogsClean();
this.notifyTestEnd("testAsyncCallTheServer");
}
}
});
$controller can be either a string or an object. If it's a string it's considered as a classpath, if it's an object it must match the bean InitModuleController. This allow to pass initArgs to the module's init method.
Aria.classDefinition({
$classpath : "test.ModuleControllerTest",
$extends : "aria.jsunit.ModuleCtrlTestCase",
$prototype : {
$controller : {
classpath : "myapp.SimpleController",
initArgs : {
dataModel : {
value : "something"
}
}
},
testModel : function () {
// Assuming that the module sets its data model to
// what was passed as initArgs
var data = this.$moduleCtrl.getData();
this.assertJsonEquals(data, {
value : "something"
});
}
}
});
Aria.classDefinition({
$classpath : "test.ModuleControllerTest",
$extends : "aria.jsunit.ModuleCtrlTestCase",
$dependencies : ["myapp.SimpleController", "aria.templates.ModuleCtrlFactory"],
$prototype : {
testAsyncModule : function () {
aria.templates.ModuleCtrlFactory.createModuleCtrl({
classpath : "myapp.SimpleController"
}, {
fn : afterModuleCreation,
scope : this
});
},
afterModuleCreation : function (result) {
if (result.error) {
this.fail("Unable to create the module controller: " + result.error);
} else {
var moduleCtrl = result.moduleCtrl;
// Start testing something
this.registerObject(moduleCtrl);
moduleCtrl.callAMethod();
this.assertEventFired("anEvent");
// End of testing, do some cleaning
this.unregisterObject(moduleCtrl);
moduleCtrl.$dispose();
this.notifyTestEnd("testAsyncModule");
}
}
}
});