如何在角茉莉试验中窥探内在功能

时间:2016-06-28 19:57:10

标签: angularjs jasmine

有没有办法监视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){
                       ...
            }

        }

    })();

1 个答案:

答案 0 :(得分:0)

您的代码结构不是特别适合启用测试。最好将isMyResponse添加到MyInterceptor原型中。这样,您可以在测试中监视原型:

function MyInterceptor(...) { ... }
MyInterceptor.prototype.isMyResponse = function (...) {
  // the original isMyResponse method goes here
};

然后在测试中,您应该能够做到这样的事情:

beforeEach(inject(MyInterceptor => {
  spyOn(MyInterceptor, 'isMyResponse');
});