こんにちは、私は を使用してアプリを構築しAngularJS
ています。今は、アプリケーションの単体テストを行っています。サービス、コントローラーなどの単体テスト ケースの書き方は知っていますが、$routeChangeStart
.
app.js に次のコードがあります
app.run(function ($rootScope, $location, AuthenticationService) {
$rootScope.$on('$routeChangeStart', function () {
if (AuthenticationService.isLoggedIn()) {
$rootScope.Authenticated = 'true';
$rootScope.Identity = localStorage.getItem('identification_id');
} else {
$rootScope.Authenticated = 'false';
$rootScope.Identity = localStorage.removeItem('identification_id');
}
});
});
アプリのルーティングごとにユーザーがログインしているかどうかを確認するために、このコードを作成しました。AuthenticationService
この目的のために、次のようなサービスを作成しました。
app.factory('AuthenticationService', function (SessionService) {
return {
isLoggedIn: function () {
return SessionService.get('session_id');
}
};
});
そして、私のセッションサービスは次のようになります。
app.factory('SessionService', function () {
return {
get: function (key) {
return localStorage.getItem(key);
}
};
});
Jasmine
テストケースを書くために使用し、Istanbul
コードカバレッジに使用しています。を使用してテストを実行するGrunt
と、app.js で次のようなものが得られます。
これは、この特定のコードのテスト ケースの書き方がわからないため、テスト ケースでこれらのステートメントをカバーしていないためです。助言がありますか?