Skip to content

Loading…

Improve ModuleCtrlTestCase #407

Closed
wants to merge 1 commit into from

2 participants

@piuccio

Improve ModuleCtrlTestCase by providing automatic creation and disposal of tested module.

By specifying $controller in the test's $prototype an instance of that class is generated before running the test.

The tester can then access the two variables $moduleCtrl and $moduleCtrlPrivate.

The public interface is also registered for asserting its logs, so there's no need to call anymore registerObject.

The test makes sure that objects are created and disposed correctly off-loading some work from the developer.

@divdavem divdavem was assigned
@piuccio piuccio feat #407 improve ModuleCtrlTestCase
Improve ModuleCtrlTestCase by providing automatic creation and disposal of tested module.
By specifying `$controller` in the test's `$prototype` an instance of that class is generated before running the test.
The tester can then access the two variables `$moduleCtrl` and `$moduleCtrlPrivate`.
The public interface is also registered for asserting its logs, so there's no need to call anymore `registerObject`.

Close #407
9b83446
@divdavem divdavem added a commit to divdavem/ariatemplates that referenced this pull request
@piuccio piuccio feat #407 improve ModuleCtrlTestCase
Improve ModuleCtrlTestCase by providing automatic creation and disposal of tested module.
By specifying `$controller` in the test's `$prototype` an instance of that class is generated before running the test.
The tester can then access the two variables `$moduleCtrl` and `$moduleCtrlPrivate`.
The public interface is also registered for asserting its logs, so there's no need to call anymore `registerObject`.

Close #407
c71eea3
@piuccio piuccio added a commit that closed this pull request
@piuccio piuccio feat #407 improve ModuleCtrlTestCase
Improve ModuleCtrlTestCase by providing automatic creation and disposal of tested module.
By specifying `$controller` in the test's `$prototype` an instance of that class is generated before running the test.
The tester can then access the two variables `$moduleCtrl` and `$moduleCtrlPrivate`.
The public interface is also registered for asserting its logs, so there's no need to call anymore `registerObject`.

Close #407
c71eea3
@piuccio piuccio closed this in c71eea3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 11, 2013
  1. @piuccio

    feat #407 improve ModuleCtrlTestCase

    piuccio committed
    Improve ModuleCtrlTestCase by providing automatic creation and disposal of tested module.
    By specifying `$controller` in the test's `$prototype` an instance of that class is generated before running the test.
    The tester can then access the two variables `$moduleCtrl` and `$moduleCtrlPrivate`.
    The public interface is also registered for asserting its logs, so there's no need to call anymore `registerObject`.
    
    Close #407
View
55 src/aria/jsunit/ModuleCtrlTestCase.js
@@ -18,9 +18,9 @@
* a log of events.
*/
Aria.classDefinition({
- $classpath : 'aria.jsunit.ModuleCtrlTestCase',
- $extends : 'aria.jsunit.TestCase',
- $dependencies : ['aria.jsunit.TestMsgHandler', 'aria.modules.RequestMgr'],
+ $classpath : "aria.jsunit.ModuleCtrlTestCase",
+ $extends : "aria.jsunit.TestCase",
+ $dependencies : ["aria.jsunit.TestMsgHandler", "aria.modules.RequestMgr", "aria.templates.ModuleCtrlFactory"],
$constructor : function () {
this.$TestCase.constructor.call(this);
@@ -34,23 +34,64 @@ Aria.classDefinition({
// we pass a reference to this so the TestMsgHandler can access it during initialization
aria.core.IOFiltersMgr.addFilter({
- classpath : 'aria.jsunit.TestMsgHandler',
+ classpath : "aria.jsunit.TestMsgHandler",
initArgs : this
});
},
-
$destructor : function () {
aria.core.IOFiltersMgr.removeFilter({
- classpath : 'aria.jsunit.TestMsgHandler',
+ classpath : "aria.jsunit.TestMsgHandler",
initArgs : this
});
this.cxLogs = null;
this.evtLogs = null;
+ if (this.$moduleCtrl) {
+ this.unregisterObject(this.$moduleCtrl);
+ this.$moduleCtrl = null;
+ }
+ if (this.$moduleCtrlPrivate) {
+ this.$moduleCtrlPrivate.$dispose();
+ this.$moduleCtrlPrivate = null;
+ }
this.$TestCase.$destructor.call(this);
},
-
+ $statics : {
+ FACTORY_ERROR : "Unable to create a module controller instance of %1"
+ },
$prototype : {
/**
+ * Override TestCase run method to load a module controller
+ * @override
+ */
+ run : function () {
+ if (this.$controller) {
+ var description = aria.utils.Type.isString(this.$controller) ? {
+ classpath : this.$controller
+ } : this.$controller;
+ // We want to automatically load a module controller
+ aria.templates.ModuleCtrlFactory.createModuleCtrl(description, {
+ fn : this._goOnWithRun,
+ scope : this,
+ args : description
+ }, false);
+ } else {
+ this.$TestCase.run.call(this);
+ }
+ },
+
+ _goOnWithRun : function (result, description) {
+ if (result.error) {
+ this.$logError(this.FACTORY_ERROR, [description.classpath]);
+ } else {
+ this.$moduleCtrl = result.moduleCtrl;
+ this.$moduleCtrlPrivate = result.moduleCtrlPrivate;
+ // I obviously expect to listen to this object
+ this.registerObject(this.$moduleCtrl);
+ }
+ this.$TestCase.run.call(this);
+ },
+
+ /**
* Clears both connection logs and call the parent's implementation.
*/
clearLogs : function () {
View
1 test/aria/jsunit/JsunitTestSuite.js
@@ -22,5 +22,6 @@ Aria.classDefinition({
this.addTests("test.aria.jsunit.ModuleCtrlTestCaseTest");
this.addTests("test.aria.jsunit.AssertTest");
this.addTests("test.aria.jsunit.TestCaseTest");
+ this.addTests("test.aria.jsunit.ModuleControllerTestCase");
}
});
View
51 test/aria/jsunit/ModuleControllerTestCase.js
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2012 Amadeus s.a.s.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+Aria.classDefinition({
+ $classpath : "test.aria.jsunit.ModuleControllerTestCase",
+ $extends : "aria.jsunit.ModuleCtrlTestCase",
+ $dependencies : ["test.aria.jsunit.mock.RedirectToFile"],
+ $prototype : {
+ $controller : "test.aria.jsunit.mock.SimpleController",
+
+ setUp : function () {
+ aria.core.IOFiltersMgr.addFilter("test.aria.jsunit.mock.RedirectToFile");
+ },
+
+ tearDown : function () {
+ aria.core.IOFiltersMgr.removeFilter("test.aria.jsunit.mock.RedirectToFile");
+ },
+
+ testAsyncCallTheServer : function () {
+ this.$moduleCtrl.processCommand({
+ fn : this.after,
+ scope : this
+ });
+ },
+
+ after : function () {
+ this.assertEventFired("responseReceived");
+ // This checks that there are no response error
+ this.assertLogsClean();
+ var data = this.$moduleCtrl.getData();
+ this.assertJsonEquals(data, {
+ response : {
+ one : 1
+ }
+ });
+ this.notifyTestEnd("testAsyncCallTheServer");
+ }
+ }
+});
View
27 test/aria/jsunit/mock/RedirectToFile.js
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2012 Amadeus s.a.s.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * This mock Msg Handler is used by test cases in test.aria.jsunit
+ */
+Aria.classDefinition({
+ $classpath : "test.aria.jsunit.mock.RedirectToFile",
+ $extends : "aria.core.IOFilter",
+ $prototype : {
+ onRequest : function (req) {
+ this.redirectToFile(req, req.url + ".json");
+ }
+ }
+});
View
48 test/aria/jsunit/mock/SimpleController.js
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2012 Amadeus s.a.s.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+Aria.classDefinition({
+ $classpath : "test.aria.jsunit.mock.SimpleController",
+ $extends : "aria.templates.ModuleCtrl",
+ $implements : ["test.aria.jsunit.mock.IMockModule"],
+ $dependencies : ["aria.modules.requestHandler.JSONRequestHandler"],
+ $constructor : function () {
+ this.$ModuleCtrl.constructor.apply(this, arguments);
+
+ this.$requestHandler = new aria.modules.requestHandler.JSONRequestHandler();
+ },
+ $destructor : function () {
+ this.$requestHandler.$dispose();
+
+ this.$ModuleCtrl.$destructor.apply(this, arguments);
+ },
+ $prototype : {
+ $publicInterfaceName : "test.aria.jsunit.mock.IMockModule",
+
+ processCommand : function (callback) {
+ this.submitJsonRequest("processCommand", null, {
+ fn : this.onResponse,
+ scope : this,
+ args : callback
+ });
+ },
+
+ onResponse : function (response, callback) {
+ this.setData(response);
+ this.$raiseEvent("responseReceived");
+ this.$callback(callback);
+ }
+ }
+});
View
3 test/aria/jsunit/mock/processCommand.json
@@ -0,0 +1,3 @@
+{
+ one : 1
+}
Something went wrong with that request. Please try again.