What is angular-mocks?
The angular-mocks package provides mocking and testing utilities for AngularJS applications. It includes modules, services, and methods to facilitate unit testing and end-to-end testing of AngularJS code.
What are angular-mocks's main functionalities?
Mocking HTTP Requests
This feature allows you to mock HTTP requests in your AngularJS application. The code sample demonstrates how to use $httpBackend to intercept a GET request to '/api/data' and respond with a status code of 200 and a JSON object.
angular.module('myApp', ['ngMockE2E'])
.run(function($httpBackend) {
$httpBackend.whenGET('/api/data').respond(200, { key: 'value' });
});
Injecting and Mocking Services
This feature allows you to inject and mock services in your unit tests. The code sample shows how to inject MyService and $httpBackend, set up an expectation for an HTTP GET request, and verify the response.
describe('MyService', function() {
var MyService, $httpBackend;
beforeEach(module('myApp'));
beforeEach(inject(function(_MyService_, _$httpBackend_) {
MyService = _MyService_;
$httpBackend = _$httpBackend_;
}));
it('should fetch data', function() {
$httpBackend.expectGET('/api/data').respond(200, { key: 'value' });
MyService.getData().then(function(response) {
expect(response.data.key).toBe('value');
});
$httpBackend.flush();
});
});
Spying on Methods
This feature allows you to spy on methods to check if they have been called and with what arguments. The code sample demonstrates how to spy on the getData method of MyService and verify that it has been called when MyController is instantiated.
describe('MyController', function() {
var $controller, $rootScope, MyService;
beforeEach(module('myApp'));
beforeEach(inject(function(_$controller_, _$rootScope_, _MyService_) {
$controller = _$controller_;
$rootScope = _$rootScope_;
MyService = _MyService_;
}));
it('should call MyService.getData', function() {
spyOn(MyService, 'getData').and.callThrough();
var $scope = $rootScope.$new();
$controller('MyController', { $scope: $scope });
expect(MyService.getData).toHaveBeenCalled();
});
});
Other packages similar to angular-mocks
jasmine
Jasmine is a behavior-driven development framework for testing JavaScript code. It provides functions to help with structuring tests and making assertions. Unlike angular-mocks, Jasmine is not specific to AngularJS but can be used with any JavaScript code.
karma
Karma is a test runner that allows you to execute JavaScript code in multiple real browsers. It is often used in conjunction with Jasmine and angular-mocks for running AngularJS tests. Karma provides a more comprehensive testing environment compared to angular-mocks alone.
protractor
Protractor is an end-to-end test framework for Angular and AngularJS applications. It is built on top of WebDriverJS and is designed to test Angular applications in a real browser. Protractor offers more advanced end-to-end testing capabilities compared to angular-mocks.
packaged angular-mocks
This repo is for distribution on npm
and bower
. The source for this module is in the
main AngularJS repo.
Please file issues and pull requests against that repo.
Install
You can install this package either with npm
or with bower
.
npm
npm install angular-mocks
You can require
ngMock modules:
var angular = require('angular');
angular.module('myMod', [
require('angular-animate'),
require('angular-mocks/ngMock'),
require('angular-mocks/ngAnimateMock')
]);
bower
bower install angular-mocks
The mocks are then available at bower_components/angular-mocks/angular-mocks.js
.
Documentation
Documentation is available on the
AngularJS docs site.
License
The MIT License
Copyright (c) 2010-2015 Google, Inc. http://angularjs.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.