确保在 beforeEach 中包含 ngMockE2E 模块
如果不whenGET
调用 $browser 服务 mock 将不会被实例化,并且返回值不会设置passThrough
函数
beforeEach(function() {
module('yourModule');
module('ngMockE2E'); //<-- IMPORTANT!
inject(function(_$httpBackend_) {
$httpBackend = _$httpBackend_;
$httpBackend.whenGET('somefile.html').passThrough();
});
});
angular-mocks.js 中设置的位置:
有问题的源代码在 $httpBackend mock 的when
函数中:
function (method, url, data, headers) {
var definition = new MockHttpExpectation(method, url, data, headers),
chain = {
respond: function(status, data, headers) {
definition.response = createResponse(status, data, headers);
}
};
if ($browser) {
chain.passThrough = function() {
definition.passThrough = true;
};
}
definitions.push(definition);
return chain;
}