请参阅下面的代码示例,我有一个量角器测试,可以通过角度量角器作为GULP任务运行。
我希望能够为所有块模拟一些REST调用,但也可以访问特定块的httpBackend。
我在共享$ httpBackend变量时遇到了麻烦:
代码示例
describe('Train Station Search Component', function() {
beforeEach(function() {
//Mock any REST calls here.
browser.addMockModule('httpBackendMock', function() {
angular.module('httpBackendMock', ['MyApp', 'ngMockE2E'])
.run(function($httpBackend) {
//Mock call relevant to both it blocks.
$httpBackend.whenPOST(/^(.*\/api\/search)/).respond({...});
});
});
);
it('should check one piece of functionality', function() {
//Expect call with data relevant only for this it block.
$httpBackend.expectPOST(...);
});
it('should check another piece of functionality', function() {
//Expect call with data relevant only for this it block.
$httpBackend.expectPOST(...);
});
};
任何帮助表示感谢。
答案 0 :(得分:0)
您可以使用$ injector注入$ httpBackEnd服务。
var $httpBackend
beforeEach(inject(function($injector) {
// Set up the mock http service responses
$httpBackend = $injector.get('$httpBackend');
});
浏览链接https://docs.angularjs.org/api/ngMock/service/$httpBackend