diff options
| author | Igor Minar | 2011-01-26 20:12:23 -0800 | 
|---|---|---|
| committer | Igor Minar | 2011-01-26 20:12:23 -0800 | 
| commit | 3f0a37f380e262fa86689f06c0801ae54d8a0983 (patch) | |
| tree | 3d19cd728ab89340785ab11332d9bc896cacdca6 /test | |
| parent | f5d08963b0c836b10133a94d03a81254242661eb (diff) | |
| download | angular.js-3f0a37f380e262fa86689f06c0801ae54d8a0983.tar.bz2 | |
add missing test/mocks.js
Diffstat (limited to 'test')
| -rw-r--r-- | test/mocks.js | 67 | 
1 files changed, 67 insertions, 0 deletions
| 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: + * + * <pre> + *   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']); + *   }); + * </pre> + * + * 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: + * + * <pre> + *   var scope = angular.scope(null, {'$exceptionHandler': $exceptionHandlerMockFactory}); + * </pre> + * + */ +function $exceptionHandlerMockFactory() { +  function mockHandler(e) { +    mockHandler.errors.push(e); +  } +  mockHandler.errors = []; + +  return mockHandler; +}
\ No newline at end of file | 
