実行時にHTTPエンドポイントで設定を実行するにはアプリが必要です。AngularJSを使ってアプリケーションをロードするときのユニットテスト
私はそれを行うためのシンプルなサービスを書いた:
module.factory('config', function ($http, analytics) {
return {
load: function() {
$http.get('/config').then(function (response) {
analytics.setAccount(response.googleAnalyticsAccount);
});
}
}
});
次へ]を、私は私のアプリモジュールの実行ブロックにこのモジュールを呼び出す:
angular.module('app').***.run(function(config) {
config.load();
});
アプリがあるときは、すべてがうまく機能しています"エラー:予期しない要求:GET/config"
私はそれが何を意味しているのか知っていますが、実行ブロックから起こったときにそれを模擬する方法がわかりません。あなたの助けのための
おかげ各
beforeEach(angular.mock.module('app'));
は$ httpBackendを模擬するために、これをしようとしました前にこれを呼び出す仕様
を追加する
EDIT:
beforeEach(inject(function($httpBackend) {
$httpBackend.expectGET('/config').respond(200, {'googleAnalyticsAccount':});
angular.mock.module('app')
$httpBackend.flush();
}));
しかし得ました:
AngularJS私は角度チーム、問題がなくなっているからイゴールにより、AngularJS 1.0.6にアップデートをお勧めしてきたので1.0.6への更新からは
TypeError: Cannot read property 'stack' of null
at workFn (/Users/arnaud/workspace/unishared-dredit/test/lib/angular/angular-mocks.js:1756:55)
TypeError: Cannot read property 'stack' of null
at workFn (/Users/arnaud/workspace/unishared-dredit/test/lib/angular/angular-mocks.js:1756:55)
TypeError: Cannot read property 'stack' of null
at workFn (/Users/arnaud/workspace/unishared-dredit/test/lib/angular/angular-mocks.js:1756:55)
EDITが、今私は今持っていますこれは、より "正常"と聞こえるが、私はまだそれを動作させる方法を理解できない。
Error: Injector already created, can not register a module!
'run()'をテストするときにもこのエラーが発生します – halfcube
私は最終的にアプリケーションルートコントローラでテスト可能にするために移動しました。 –