2016-08-16 2 views
0

저는 TDD를 처음 사용하고 각도 j에서 authInceptceptor (저는 chai/mocha/sinon을 사용할 수 있습니다)를 테스트하고 있습니다. 각도 j에는 두 개의 함수, 요청 및 responseError가 있습니다. . 나는 요청 함수를 성공적으로 테스트했지만 401 (인증되지 않은) 오류를 조롱하는 방법 (문서를 샅샅이 뒤 졌는지)을 모르겠습니다. 여기에 요격기가 있습니다 :각도 js karma/chai - 인증 오류 모의

export default function AuthInterceptor($q, $injector, $log) { 
    'ngInject'; 

    return { 
    request(config) { 
     let AuthService = $injector.get('AuthService'); 
     if (!config.bypassAuthorizationHeader) { 
     if (AuthService.jwtToken) { 
      config.headers.Authorization = `Bearer ${AuthService.jwtToken}`; 
     } else { 
      $log.warn('Missing JWT', config); 
     } 
     } 
     return config || $q.when(config); 
    }, 
    responseError(rejection) { 
     let AuthService = $injector.get('AuthService'); 
     if (rejection.status === 401) { 
     AuthService.backToAuth(); 
     } 
     return $q.reject(rejection); 
    } 
    }; 

} 

여기 네 가지 테스트가 있습니다. 내가, 내가 그 "가"블록에 주석을 붙어 추가하고 여기서 처음 세 '가'블록이 성공적으로 패스, 네 번째는 다음과 같습니다

import angular from 'angular'; 
import AuthInterceptor from './auth.interceptor' 

describe('Auth interceptor test',() => { 

    describe('AuthInterceptor test',() => { 
    let $httpBackend, $http, authInterceptor = AuthInterceptor(); 

    beforeEach(angular.mock.module(($httpProvider, $provide) => { 
     $httpProvider.interceptors.push(AuthInterceptor); 
     $provide.factory('AuthService',() => ({ 
     jwtToken: "hello", 
     backtoAuth: angular.noop 
     })); 
    })); 

    beforeEach(inject(function($injector) { 
     $httpBackend = $injector.get('$httpBackend'); 
     $http = $injector.get('$http'); 
    })) 


    it('should have a request function',() => { 
     let config = {}; 
     expect(authInterceptor.request).to.be.defined; 
     expect(authInterceptor.request).to.be.a('function'); 

    }) 

    it('the request function should set authorization headers', (done) => { 
     $httpBackend.when('GET', 'http://jsonplaceholder.typicode.com/todos') 
     .respond([{ 
      id: 1, 
      title: 'Fake title', 
      userId: 1 
     }]); 
     $http.get('http://jsonplaceholder.typicode.com/todos').then(function(transformedResult) { 

     expect(transformedResult.config.headers.Authorization).to.be.defined; 
     expect(transformedResult.config.headers.Authorization).to.contain('Bearer') 
     done(); 
     }) 
     $httpBackend.flush(); 
    }); 

    it('should have a responseError function',() => { 
     expect(authInterceptor.responseError).to.be.defined; 
     expect(authInterceptor.responseError).to.be.a('function'); 
     //TODO: test return value 
     // see that AuthService.backToAuth() 
    }) 

    it('the error function should call backtoAuth', (done) => { 
    //a url that doesn't give me a 401 like I'm hoping. 
    $httpBackend.when('POST', 'https://wwws.mint.com/overview.event').respond([ 
    //what do I do here? 
    ]) 
    $http.post('https://wwws.mint.com/overview.event').then(function(transformedResult) { 

    console.log("success", transformedResult); 
    done(); 
    }, function(error){ 
    // I can't seem to get in here. if I can, the responseError should be called, which in turn calls backToAuth... 
    console.log("error", error); 
    done(); 
    }) 
    $httpBackend.flush(); 
}); 

답변

1

첫 번째 respond 매개 변수는 status입니다 그것이 있어야

$httpBackend.when('POST', 'https://wwws.mint.com/overview.event').respond(401); 

이 Sinon/재스민 스파이/대신 스텁 방법에 noops의 스텁을 사용하는 것이 좋다, 그래서 그들의 통화를 테스트 할 수 있습니다 :

var sandbox; 

beforeEach(() => { 
    sandbox = sinon.sandbox.create(); 
}); 

afterEach(() => { 
    sandbox.restore(); 
}); 

beforeEach(angular.mock.module(($httpProvider, $provide) => { 
    $httpProvider.interceptors.push(AuthInterceptor); 
    $provide.factory('AuthService',() => ({ 
    jwtToken: "hello", 
    backtoAuth: sandbox.stub(); 
    })); 
}));