
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
angular-spies
Advanced tools
Super easy spy setup and injection library.
If you ever wrote Angular tests, especially in TDD style (Test Driven Development), you know you need Spies (also known by their other misused name: mocks).
You need them to isolate your units (services, controllers, etc...) from their dependencies (other services).
Usually it involves tons of boilerplate code but NO MORE!
jasmine.createSpy
)npm install angular-spies
or
bower install angular-spies
angular.spyOnService( serviceName, [ optionalParentSpy, ...])
angular
.spyOnService('productService')
.methods( methodName, ...)
angular
.spyOnService('productService')
.methods('getProducts', 'saveProducts')
injectSpy( serviceName ) { }
var productCtrl,
productServiceSpy;
beforeEach( injectSpy( function(productService) {
productServiceSpy = productService;
}));
it ('should get products', function(){
var fakeProducts = ['product1', 'product2'];
productServiceSpy.getProducts.and.returnValue( fakeProducts );
productCtrl.loadProducts();
expect(productServiceSpy.getProducts).toHaveBeenCalled();
});
.asyncMethods( methodName, ...)
angular
.spyOnService('productService')
.methods('someSyncMethod')
.asyncMethods('getProducts', 'saveProducts')
Angular spies uses $q in the background to create both the promise as the return value, and exposes its deferred object, and allows you to control the promise's state.
it ('should get products async', function(){
var fakeProducts = ['product1', 'product2'];
var returnedProducts;
productServiceSpy.getDeferred('getProducts').resolve( fakeProducts );
productServiceSpy.getProducts().then(function (products) {
returnedProducts = products;
});
$rootScope.flush();
expect(productServiceSpy.getProducts).toHaveBeenCalled();
expect(returnedProducts).toBe(fakeProducts);
});
Lets say for example that you have a generic spy for a data library, with all the CRUD methods defined already:
angular
.spyOnService('dataService')
.asyncMethods('create', 'read', 'update', 'delete')
You can use the second parameter of the spyOnService
method to declare its parent spies, just add second parameter as an array of spy names you want to extend from (Like angular.module(moduleName, [depenedencies])
)
angular
.spyOnService('productService', ['dataService'])
.asyncMethods('createProductByName')
Now the spy of productService
will have 5 async methods - create
, read
, update
, delete
and createProductByName
See the Example Project to get a quick look on how to test your controllers.
Copyright (c) 2015 HiRez.io
Licensed under the MIT License.
FAQs
Spies For Your Angular Tests TDD
The npm package angular-spies receives a total of 1 weekly downloads. As such, angular-spies popularity was classified as not popular.
We found that angular-spies 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
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.