0

作为测试的新手,我做了一些愚蠢的假设,因此我的测试用例没有运行。

我的拦截器工厂看起来像:

angular.module('svcs')
    .factory('authorizationInterceptor', function ($q) {
        var myserver = '',
             serverType = '';
        var myActionTransformer = function (config) {
            .....
            .....
            return ...;
        };
        return {
            responseError: function (response) {
                if (response.status === 403) {
                    console.log('Res : ',response);
                    response.data = 'You are not authorized to ' +
                        myActionTransformer(response.config);
                }
                return $q.reject(response);
            }
        };
    });

现在要测试相同的内容,我写道:

describe.only('svcs', function () {
    var authorizationInterceptor,authorizationInterceptorSpy,$q,actionTransformerSpy;

    beforeEach(function () {
        module('svcs');

        inject(function (_authorizationInterceptor_,_$q_) {
            authorizationInterceptor = _authorizationInterceptor_;
            $q = _$q_;
        });
    });

    describe('authorizationInterceptor', function () {

    beforeEach(function () {
        sinon.spy(authorizationInterceptor, 'responseError').andCallThrough();
        actionTransformerSpy = sinon.spy(authorizationInterceptor, 'myActionTransformer');
    });

    afterEach(function () {
        authorizationInterceptorSpy.restore(); 
    });

    it('should call actionTransformerSpy', function () {
        authorizationInterceptor.responseError({ code: 403});
        expect(actionTransformerSpy).to.be.called;
        expect(actionTransformerSpy).calledOnce;
    });

});

茉莉花CallThrough() is not working and it's not calling myActionTransformer,这正是我想要测试的。

我越来越undefined is not a constructor (evaluating 'sinon.spy(authorizationInterceptor, 'responseError').andCallThrough()' )

我是新来的。请帮忙解释一下。

4

0 回答 0