Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
angular-mocks
Advanced tools
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.
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();
});
});
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 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 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.
This package contains the legacy AngularJS (version 1.x). AngularJS support has officially ended as of January 2022. See what ending support means and read the end of life announcement.
See @angular/core
for the actively supported Angular.
You can install this package either with npm
or with bower
.
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 install angular-mocks
The mocks are then available at bower_components/angular-mocks/angular-mocks.js
.
Documentation is available on the AngularJS docs site.
The MIT License
Copyright (c) 2022 Google LLC
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.
FAQs
AngularJS mocks for testing
The npm package angular-mocks receives a total of 138,575 weekly downloads. As such, angular-mocks popularity was classified as popular.
We found that angular-mocks demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.