有没有办法监视isMyResponse
(function() {
'use strict';
angular
.module('mymodule')
.factory('MyInterceptor', MyInterceptor);
MyInterceptor.$inject = [...];
function MyInterceptor(...) {
var self = this;
self.isMyResponse = isMyResponse;
return {
request: {},
response: function (response) {
if(self.isMyResponse(response)){
}
}
};
function isMyResponse(response){
...
}
}
})();
答案 0 :(得分:0)
您的代码结构不是特别适合启用测试。最好将isMyResponse
添加到MyInterceptor
原型中。这样,您可以在测试中监视原型:
function MyInterceptor(...) { ... }
MyInterceptor.prototype.isMyResponse = function (...) {
// the original isMyResponse method goes here
};
然后在测试中,您应该能够做到这样的事情:
beforeEach(inject(MyInterceptor => {
spyOn(MyInterceptor, 'isMyResponse');
});