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.
Angular Mocks v1.2.21
Npm package for angular-mocks.js
, The source for this module is in the
main AngularJS repo.
Please file issues and pull requests against that repo.
Official documentation is available on the
AngularJS docs site.