我目前正在设置使用OpenLayers的测试,我希望能够在Karma单元测试中模拟Bing映射等服务的请求。我曾尝试使用Sinon来模拟XHR请求,但意识到它是generating these requests by dynamically creating and injecting a script
tag into the DOM head
and binding the JSONP response。
我在TravisCI上使用PhantomJS和本地Chrome进行测试,我试图找出如何提供我自己的JSONP响应来代替将此标记注入head
元素的请求。这将减少API请求,加速测试并避免在单元测试期间发生的潜在API /网络问题。
目前,我已使用以下设置尝试过SinonJS。
server = sinon.fakeServer.create();
server.respondWith('GET', bingRequestUrl,
[200, {"Content-Type": "application/x-javascript; charset=utf-8"},
bingFakeResponse
]);
$scope.$digest();
$timeout.flush();
server.restore();
我的想法是,我只会在处理期间嘲笑请求,然后恢复正常功能。
是否有办法使用Sinon实现我需要的功能,或者只是模拟XHR请求而不是来自此示例中显示的DOM操作的请求。