【发布时间】:2013-10-06 05:52:39
【问题描述】:
您好,我正在使用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 中出现了类似的内容;
这是因为我没有在我的测试用例中涵盖这些语句,因为我不知道如何为这段特定的代码编写测试用例。有什么建议吗?
【问题讨论】:
-
您的应用中是否定义了任何路由?
标签: angularjs jasmine istanbul