0

Being pretty new to testing, I am making some silly assumptions and hence my test cases are not running.

My interceptor factory looks something like:

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);
            }
        };
    });

Now to test the same, I wrote:

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;
    });

});

Jasmine CallThrough() is not working and it's not calling myActionTransformer and that's exactly what I want to test.

I am getting undefined is not a constructor (evaluating 'sinon.spy(authorizationInterceptor, 'responseError').andCallThrough()' ).

I am new to this. Please help and explain a little.

StrugglingCoder
  • 4,781
  • 16
  • 69
  • 103

0 Answers0