npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

angular-spies

v1.0.5

Published

Spies For Your Angular Tests TDD

Downloads

17

Readme

angular-spies

Status: Build Status npm version npm downloads

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!

Dependencies

  • Angular 1.x
  • angular-mocks 1.x (same version as Angular)
  • Jasmine 2.x and above (spies are jasmine.createSpy)

Installation

npm install angular-spies

or

bower install angular-spies

Usage

Define a Spy

angular.spyOnService( serviceName, [ optionalParentSpy, ...])

angular
	.spyOnService('productService')

Defining Methods

.methods( methodName, ...)

angular
	.spyOnService('productService')
	.methods('getProducts', 'saveProducts')

Inject a Spy in your test

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();
});

Async Methods (Return Promises)

.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);
	
});

Extending a Previously Defined Spy

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

Example Project

See the Example Project to get a quick look on how to test your controllers.

License

Copyright (c) 2015 HiRez.io

Licensed under the MIT License.