嗨,我正在使用构建一个应用程序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 中得到类似的东西;
这是因为我没有在测试用例中涵盖这些语句,因为我不知道如何为这段特定的代码编写测试用例。有什么建议么?