AngularJS中$routeChangeStart的Jasmine单元测试用例
大家好,我正在使用AngularJS中$routeChangeStart的Jasmine单元测试用例,angularjs,jasmine,istanbul,Angularjs,Jasmine,Istanbul,大家好,我正在使用AngularJS构建一个应用程序,现在我开始对我的应用程序进行单元测试。我知道如何为服务、控制器等编写单元测试用例,但我不知道如何为$routeChangeStart编写单元测试用例 我的app.js中有以下代码 app.run(function ($rootScope, $location, AuthenticationService) { $rootScope.$on('$routeChangeStart', function () { if (A
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
编写测试用例,并使用伊斯坦布尔
进行代码覆盖。当我使用Grunt
运行测试时,我在app.js中得到了类似的结果
这是因为我没有在我的测试用例中涵盖这些语句,因为我不知道如何为这段特定的代码编写测试用例。有什么建议吗?每次加载模块时运行
块,以便在测试期间注册侦听器。您只需要实际发送该事件,以便测试其中的代码。像这样的事情应该可以做到:
it("should test the $routeChangeStart listener", inject(function($rootScope) {
$rootScope.$broadcast("$routeChangeStart");
//expects for the listener
}));
有关如何测试事件的一般信息,请参阅。测试$rootScope.$on(“$routeChangeStart”…
)的一个好方法可以在angularjs本身的单元测试中找到。它是一个极好的知识来源,因为每个功能都经过测试,所以有可能找到好的解决方案。这就是单元测试如此伟大的原因,不是吗
下面的测试(取自angular head 1.2.x-最新链接)非常有效,您只需适应测试(因为代码中已经有事件处理程序):
您的应用程序中是否定义了任何路线?
'use strict';
describe('$route', function() {
var $httpBackend;
beforeEach(module('ngRoute'));
beforeEach(module(function() {
return function(_$httpBackend_) {
$httpBackend = _$httpBackend_;
$httpBackend.when('GET', 'Chapter.html').respond('chapter');
$httpBackend.when('GET', 'test.html').respond('test');
$httpBackend.when('GET', 'foo.html').respond('foo');
$httpBackend.when('GET', 'baz.html').respond('baz');
$httpBackend.when('GET', 'bar.html').respond('bar');
$httpBackend.when('GET', 'http://example.com/trusted-template.html').respond('cross domain trusted template');
$httpBackend.when('GET', '404.html').respond('not found');
};
}));
it('should route and fire change event', function() {
var log = '',
lastRoute,
nextRoute;
module(function($routeProvider) {
$routeProvider.when('/Book/:book/Chapter/:chapter',
{controller: angular.noop, templateUrl: 'Chapter.html'});
$routeProvider.when('/Blank', {});
});
inject(function($route, $location, $rootScope) {
$rootScope.$on('$routeChangeStart', function(event, next, current) {
log += 'before();';
expect(current).toBe($route.current);
lastRoute = current;
nextRoute = next;
});
$rootScope.$on('$routeChangeSuccess', function(event, current, last) {
log += 'after();';
expect(current).toBe($route.current);
expect(lastRoute).toBe(last);
expect(nextRoute).toBe(current);
});
$location.path('/Book/Moby/Chapter/Intro').search('p=123');
$rootScope.$digest();
$httpBackend.flush();
expect(log).toEqual('before();after();');
expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', p:'123'});
log = '';
$location.path('/Blank').search('ignore');
$rootScope.$digest();
expect(log).toEqual('before();after();');
expect($route.current.params).toEqual({ignore:true});
log = '';
$location.path('/NONE');
$rootScope.$digest();
expect(log).toEqual('before();after();');
expect($route.current).toEqual(null);
});
});