Code Monkey home page Code Monkey logo

ngresourcemock's Introduction

ngResourceMock

This software is in the early state of development, please use with caution. Why

This project aims to simplify testing of applications that make use of the ngResource. When testing the use of ngResource there are two main approaches: mocking the resource itself or using $httpBackend to mock the http responses. Both of those approaches have significant drawbacks. Mocking of the resource is quite a tedious task. The same can be said for mocking the http responses. Moreover it crosses the abstraction layer, as we not only test our code, but the ngResource itself. It makes the tests unnecessarily complex and dificult to read.

ngResourceMock simplifies the task by providing a simple, yet flexible mocking API.

Here is a simple example usage:

it('sould resolve the call with given data', function() {
  var expectedResult = {
    test: 'test'
  };
  TestResource.expectSave(testData).resolve(expectedResult);
  var actualResult = TestResource.save(testData);
  TestResource.flush();
  expect(angular.equals(actualResult, expectedResult)).toBe(true);
});

Please note: the comparison is accomplished using the angular.equals method as it ignores properties starting with $. The resulting object, just like with ngResource has $promise and $resolved properties that you can use.

How

The ngResourceMock is implemented in the form of a decorator on the $resource factory. To use it you need to add ng-resource-mock.js to the files loaded by karma.

The api is similar to the API of the $httpBackend. The mock adds following methods to the resource service/instance:

  • whenAction - e.g. (whenSave, whenQuery, etc.) creates when expectation. Accepts two arguments that are interpreted - the same way they would be interpreted on a real action call - as either request parameters or the post data. The arguments serve as patterns that the requests are matched against. If argument is falsy the expectation matches requests regardless. To check against null or undefined values please use the null and undefined fields of the resource eg.

    MockedResource.whenSave(MockedResource.null)

    The call returns an object with following methods:

    • resolve(data) - resolves the request with given data
    • resolveSame() - resolves the request with the post data passed to the action method
    • reject(data) - rejects the request with given data

    Moreover each of the methods results has andFlush() method.

  • expectAction - creates expectation. Similar to whenAction.

  • flush - flushes all unresolved requests

  • flushAll - available only on a class object. Flushes all pending requests on the class object and all the instance objects

  • verifyNoOutstandingExpectation

  • setMockingConfiguration(config) - method used to configure the mock. The config object has following properties:

    • autoFlush - default false. Specifies if the mock should automatically flush the results after the method call.
    • instanceExpectations - default true, when set each resource instance keeps track of it's expectations. If set to false all expectations are defined on the class, and are common for all instances and for static calls.

The ngResourceMock supports both instance and class usage. When used for instances it remembers and matches expectations per instance.

The ngResourceMock supports fluent API making following possible:

MockedResource.whenSave().resolveSame().andFlush();

Whishlist

  • By default the mock should delegate the method calls to the real resource
  • Support for cancelling requests

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.