1

I am currently setting up tests that utilize OpenLayers and I would like to be able to mock requests to services like Bing Maps in Karma unit tests. I've tried to use Sinon to mock XHR requests but realized it is generating these requests by dynamically creating and injecting a script tag into the DOM head and binding the JSONP response.

I am using PhantomJS on TravisCI and Chrome locally to test and I am trying to work out how provide my own JSONP response in place of the request coming from injecting this tag into the head element. This will be to reduce on API requests, speed up testing and avoid potential API/network issues occurring during unit tests.

Currently I've tried SinonJS using the following setup.

server = sinon.fakeServer.create();
server.respondWith('GET', bingRequestUrl,
    [200, {"Content-Type": "application/x-javascript; charset=utf-8"},
        bingFakeResponse
]);

$scope.$digest();
$timeout.flush();
server.restore();

The idea being that I am only mocking requests during the time they will be processed and then restoring normal functionality.

Is there are way to achieve what I need using Sinon or does it only mock XHR requests and not requests coming from DOM manipulation shown in this example.

Darren Reid
  • 2,322
  • 20
  • 25
  • Is it not possible to use https://docs.angularjs.org/api/ngMock/service/$httpBackend to mock the requests? – Praval 'Shaun' Tirubeni May 05 '15 at 06:50
  • @Praval'Shaun'Tirubeni I tried that but still had problems. I think `$httpBackend` is only mocking requests that use `$http`. – Darren Reid May 05 '15 at 07:22
  • Possible duplicate of [Modifying JSONP results before success callback](http://stackoverflow.com/questions/13649399/modifying-jsonp-results-before-success-callback) – Paul Sweatte Jun 23 '16 at 06:23

0 Answers0