From 3f0a37f380e262fa86689f06c0801ae54d8a0983 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Wed, 26 Jan 2011 20:12:23 -0800 Subject: add missing test/mocks.js --- test/mocks.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 test/mocks.js (limited to 'test') diff --git a/test/mocks.js b/test/mocks.js new file mode 100644 index 00000000..924c1bda --- /dev/null +++ b/test/mocks.js @@ -0,0 +1,67 @@ +/** + * Mock implementation of {@link angular.service.$log} that gathers all logged messages in arrays + * (one array per logging level). These arrays are exposed as `logs` property of each of the + * level-specific log function, e.g. for level `error` the array is exposed as + * `$logMock.error.logs` + * + * Please note that this is not a factory function, but rather the actual mock instance. This is + * important because it allows `beforeEach` and `afterEach` test hooks to clean up or check the + * state of `logs` arrays in between tests. + * + * Exposing the instance in this way makes this mock a singleton, which means that the instance + * becomes global state for tests. To mitigate the issue, each time the `$log` mock is registered + * with the injector, a check is performed to ensure that there are no pending logs in `logs` + * arrays. This means that if a message is logged via $log during a test, the `logs` array must be + * emptied before the test is finished. `Array#shift` method can be used for this purpose as + * follows: + * + *
+ *   it('should do some good', function() {
+ *     var scope = angular.scope(),
+ *         $log = scope.$service('$log');
+ *
+ *     //do something that triggers a message to be logged
+ *     expect($log.error.logs.shift()).toEqual(['message', 'arg1', 'arg2']);
+ *   });
+ * 
+ * + * See {@link angular.mock} for more info on angular mocks. + */ +var $logMock = { + log: function log(){ log.logs.push(arguments) }, + warn: function warn(){ warn.logs.push(arguments) }, + info: function info(){ info.logs.push(arguments) }, + error: function error(){ error.logs.push(arguments) } +}; +$logMock.log.logs = []; +$logMock.warn.logs = []; +$logMock.info.logs = []; +$logMock.error.logs = []; + +angular.service('$log', function() { + return $logMock; +}); + + +/** + * Factory that returns mock implementation of {@link angular.service.$exceptionHandler} that + * gathers all errors in an array. This array is exposed as `errors` property of the mock and can be + * accessed as `$exceptionHandler.errors`. + * + * Note that this factory is not registered with angular's injector by default (as opposed to + * `$logMock`). It is your responsibility to register this factory when you need it. Typically like + * this: + * + *
+ *   var scope = angular.scope(null, {'$exceptionHandler': $exceptionHandlerMockFactory});
+ * 
+ * + */ +function $exceptionHandlerMockFactory() { + function mockHandler(e) { + mockHandler.errors.push(e); + } + mockHandler.errors = []; + + return mockHandler; +} \ No newline at end of file -- cgit v1.2.3