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-mock-backend

v1.2.0

Published

AngularJS Mock Backend for Backend-less Development and Protractor Tests

Downloads

995

Readme

Angular Mock Backend

Build Status Coverage Status

Angular Mock Backend is

  1. An AngularJS module to mock some (or all) server requests (optionally with delay) for backend-less development.
  2. A Protractor module to help test client by mocking some (or all) server requests (optionally with delay).

This module provides functionality somewhat similar to that provided by ngMockE2E $httpBackend. But it does not use ngMockE2E $httpBackend for its implementation.

Installation

npm install angular-mock-backend --save-dev
bower install angular-mock-backend --save-dev

Example

Backend-less Development

Include provided mock-angular.js in your index.html and use vinkaga.mockBackend as your application dependency. Then include the following JavaScript

var mocks = [
    [['get', '/users', undefined, undefined, undefined], 200, {data:[
        {firstName: 'john', lastName: 'doe'},
        {firstName: 'angular', lastName: 'js'}
    ]}]
];
angular.module('vinkaga.mockBackend').constant('vinkaga.mockBackend.mock', mocks);

This will mock GET for /users with the specified data and 200ms delay. For a full working example, see example-backend-less.

Protractor Tests

In your Protractor test script, insert the following code before tests

var mock = require('angular-mockBackend');
mock.mock([
	[['get', '/users', undefined, undefined, undefined], 200, {data:[
    	{firstName: 'john', lastName: 'doe'},
    	{firstName: 'angular', lastName: 'js'}
    ]}],
]);

This will mock GET for /users with the specified data and 200ms delay in Protractor tests. For a full working example, see example-protractor.

API

Mock Definitions

Array of individual mock definitions, where each mock definition itself is an array of

[config, delay, response]

config

Either an array or function as follows

[method, url, params, data, headers]
// or
function(method, url, params, data, headers) {...}

If config definition is a function, the third parameter of mock definition (response) is ignored. Instead the value returned by the function is used in place of response.

delay

A number in milliseconds. Negative numbers will have an unpredictable effect.

response

If config is specified as a function, it's the value returned by the config function. Otherwise, it is the third parameter of the mock definition. It is treated as follows

Value | Description ------------- | ------------- object | Matching requests are mocked and delay is applied. The returned data is response.data and the returned status is response.status. truthy but not object | Matching requests are passed to the server after applying the delay. falsy | Matching requests are not affected.

Backend-less Development

For backend-less development, use mock definitions as follows

angular.module('vinkaga.mockBackend').constant('vinkaga.mockBackend.mock', <array of mock definitions>);

Protractor Tests

For Protractor tests, use mock definitions as follows

var mock = require('angular-mockBackend');
mock.mock(<array of mock definitions>);

Credits