Improve this Doc ngMock
Installation
First, download the file:
-
Google CDN e.g.
"//ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-mocks.js"
-
NPM e.g.
npm install [email protected]
-
Yarn e.g.
yarn add [email protected]
-
Bower e.g.
bower install angular-mocks#X.Y.Z
-
code.angularjs.org (discouraged for production use) e.g.
"//code.angularjs.org/X.Y.Z/angular-mocks.js"
where X.Y.Z is the AngularJS version you are running.
Then, configure your test runner to load angular-mocks.js
after angular.js
. This example uses Karma:
config.set({ files: [ 'build/angular.js', // and other module files you need 'build/angular-mocks.js', '<path/to/application/files>', '<path/to/spec/files>' ] });
Including the angular-mocks.js
file automatically adds the ngMock
module, so your tests are ready to go!
The ngMock
module provides support to inject and mock AngularJS services into unit tests. In addition, ngMock also extends various core AngularJS services such that they can be inspected and controlled in a synchronous manner within test code.
Module Components
Object
Name | Description |
---|---|
angular.mock |
Namespace from 'angular-mocks.js' which contains testing related code. |
Service
Name | Description |
---|---|
$flushPendingTasks |
Flushes all currently pending tasks and executes the corresponding callbacks. |
$verifyNoPendingTasks |
Verifies that there are no pending tasks that need to be flushed. It throws an error if there are still pending tasks. |
$exceptionHandler |
Mock implementation of |
$log |
Mock implementation of |
$interval |
Mock implementation of the $interval service. |
$animate |
Mock implementation of the |
$httpBackend |
Fake HTTP backend implementation suitable for unit testing applications that use the $http service. |
$timeout |
This service is just a simple decorator for $timeout service that adds a "flush" and "verifyNoPendingTasks" methods. |
$controller |
A decorator for |
$componentController |
A service that can be used to create instances of component controllers. Useful for unit-testing. |
Provider
Name | Description |
---|---|
$exceptionHandlerProvider |
Configures the mock implementation of |
Type
Name | Description |
---|---|
angular.mock.TzDate |
NOTE: this is not an injectable instance, just a globally available mock class of |
$rootScope.Scope |
Scope type decorated with helper methods useful for testing. These methods are automatically available on any Scope instance when |
Function
Name | Description |
---|---|
angular.mock.dump |
NOTE: This is not an injectable instance, just a globally available function. |
angular.mock.module |
NOTE: This function is also published on window for easy access. |
angular.mock.module.sharedInjector |
NOTE: This function is declared ONLY WHEN running tests with jasmine or mocha |
angular.mock.inject |
NOTE: This function is also published on window for easy access. |
browserTrigger |
This is a global (window) function that is only available when the |
© 2010–2018 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://code.angularjs.org/1.7.8/docs/api/ngMock