【发布时间】:2015-09-11 15:30:53
【问题描述】:
我将如何为以下这段代码编写测试?
$scope.$on('$locationChangeSuccess', function () {
$(".modal-backdrop").removeClass('modal-backdrop');
});
【问题讨论】:
标签: angularjs unit-testing jasmine
我将如何为以下这段代码编写测试?
$scope.$on('$locationChangeSuccess', function () {
$(".modal-backdrop").removeClass('modal-backdrop');
});
【问题讨论】:
标签: angularjs unit-testing jasmine
以下应该有效:
beforeEach(function() {
$('body').append('<div id="test" class="modal-backdrop"/>');
});
afterEach(function() {
$('#test').remove();
});
it('should remove the classname on $locationChangeSuccess', function() {
spyOn($scope, '$on').and.callThrough();
$scope.$broadcast('$locationChangeSuccess');
expect($scope.$on).toHaveBeenCalled();
expect($('#test').hasClass('modal-backdrop')).toBe(false);
});
【讨论】:
要触发处理函数,您需要在测试中触发事件。可以这样做:
$scope.$broadcast('$locationChangeSuccess');
【讨论】: