Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

Using Pact with Angular

Ronald Holshausen edited this page Aug 13, 2015 · 3 revisions

The Angular test http dependency won't allow real HTTP requests through, but you can use Pact with ngMidwayTester. ngMidwayTester provides a mock backend that allows interactions to pass through to the mock server.

To use it, add ng-midway-tester to your package.json (assuming your using NPM) and then do an npm install. You need to make sure that ./node_modules/ng-midway-tester/src/ngMidwayTester.js is included with your tests.

Then in your pact test, you need to add a before block that sets up the http back with ngMidwayTester and an after block to tear it down.

For example:

var tester, $http, $httpBackend, provider;

beforeEach(function () {
    angular.module('ourApp', ['ourApp.configuration', 'ngMockE2E']);
    tester = ngMidwayTester('ourApp');
    $http = tester.inject('$http');
    $httpBackend = tester.inject('$httpBackend');
    
    // You need to tell the backend what URLs to pass through
    $httpBackend.whenGET('http://localhost:8032/our-service/orders').passThrough();
    $httpBackend.whenPOST('http://localhost:8032/our-service/orders/authorise').passThrough();

    provider = Pact.mockService({
      consumer: 'ourApp',
      provider: 'ourProvider',
      port: 8032,
      done: function (error)
      {
        expect(error).toBe(null);
      }
    });
  });

  afterEach(function () {
    tester.destroy();
    tester = null;
  });
Clone this wiki locally