2016-08-16 18 views
1

私はTDDを初めて使用しており、テストを終了しようとしていて、何時間も拘束されています。ここで単体テストのモジュール/サービスを挿入できません

[$injector:modulerr] Failed to instantiate module AuthInterceptor due to: 
    Error: [$injector:nomod] Module 'AuthInterceptor' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. 
    http://errors.angularjs.org/1.5.8/$injector/nomod?p0=AuthInterceptor 
     at client/test/index.js:8237:13 
     at client/test/index.js:10251:18 
     at ensure (client/test/index.js:10175:39) 
     at module (client/test/index.js:10249:15) 
     at client/test/index.js:12786:23 
     at forEach (client/test/index.js:8490:21) 
     at loadModules (client/test/index.js:12770:6) 

は私のテストで:

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

describe('wire.common.services',() => { 

    describe('AuthService',() => { 
    let AuthService; 

    beforeEach(angular.mock.module(serviceModule.name)); 
    beforeEach(angular.mock.module(($provide) => { 
     $provide.factory('$q',() => ({})); 
     $provide.factory('$log',() => ({})); 
    })); 

    beforeEach(angular.mock.inject((_AuthService_) => { 
     AuthService = _AuthService_; 
    })); 

    it('should be a dummy test',() => { 
     expect(2).toEqual(2); 
    }); 

    }); 

}); 

私がテストだ実際のコード:

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.backToDAS(); 
     } 
     return $q.reject(rejection); 
    } 
    }; 

} 

私は、なぜ私は理解していない私は、次のエラーを取得しておきますこのエラーが発生しました - 私はサービスのすべての依存関係を提供し、角度のドキュメントに記載されているものに従っています。どんな助けもありがとう!

更新、これは私が一緒に行ったコードです:これはNGモジュールをロードするために失敗していることを意味

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", 
     backtoDAS: 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.backToDAS() 
    }) 

    it('the error function should call backtoDAS', (done) => { 
//the URL should be one that gives me a 401 
     $httpBackend.when('GET', 'https://wwws.mint.com/overview.event') 
     .respond([{ 
      id: 1, 
      title: 'Fake title', 
      userId: 1 
     }]); 
     $http.get('https://wwws.mint.com/overview.event').then(function(transformedResult) { 

     console.log(transformedResult); 
     done(); 
     }, function(error){ 
     console.log(error); 
     done(); 
     }) 

    }); 

    }) 
}); 

答えて

1

これは、AuthInterceptor角モジュールが定義されていないことを意味します(そして、nameに依存することは危険です)。

AuthInterceptorは、注入可能な機能ではありません。

beforeEach(angular.mock.module(($httpProvider) => { 
    $httpProvider.interceptors.push(AuthInterceptor); 
}); 

... 

it('...',() => { 
    $httpBackend.when(...).respond(...); 
    $http.get(...).then((interceptedResult) => { 
    expect(interceptedResult)... 
    }); 

    $rootScope.$digest(); 
}); 

または直接:副作用を生じる

it('...',() => { 
    let interceptor = $injector.invoke(AuthInterceptor); 
    expect(interceptor).toEqual({ 
    request: jasmine.any(Function), 
    requestError: jasmine.any(Function) 
    }); 

    var config = { headers: {} }; 
    interceptor.request(config); 
    expect(config)... 
}); 

サービス(AuthService$log)はスタブされるべきである$httpインターセプターとして、機能様式で試験することができます。

+0

あなたのすばらしい答えに感謝します。私はそれをすることができました!残りの唯一のことはresponseError関数をテストすることです、私はそのコードで質問を更新します。 authInceptceptorでリクエスト関数を嘲笑したのと同じ方法で、レスポンスエラー関数のアクションを模擬する良い方法を知っていますか? – devdropper87

+0

ここに私のフォローアップの質問です:http://stackoverflow.com/questions/38985850/angular-js-karma-chai-mock-an-authorization-error – devdropper87

0

。 :)これは、アプリケーションのブートストラップとngモジュールの最初の3つの要素の配列です:ng、['$提供'、関数($提供){...}]と私自身のアプリケーションモジュール。最初のものを読み込むときに失敗します。

私はコンソールを見て、私はそれからこのエラーメッセージをコピーしました。他にエラーはありません。なし。

特定のリンクをクリックして、それに関する具体的なアイデアは得られないことを願っています。残念ながら、私は他のリソースを使い果たした後、このGitHubの問題を追加しました。私は現在、角度コードをデバッグして、さらに進んでいます。

関連する問題