作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在对 Controller 进行单元测试,并且想测试事件处理程序。假设我的 Controller 看起来像:
myModule.controller('MasterController', ['$scope', function($scope){
$scope.$on('$locationChangeSuccess', function() {
$scope.success = true;
});
}]);
我会在 Jasmine 测试中广播该内容吗?我会发出它吗?有公认的标准吗?
最佳答案
我想出的解决方案如下:
describe('MasterController', function() {
var $scope, $rootScope, controller, CreateTarget;
beforeEach(function() {
inject(function($injector) {
$rootScope = $injector.get('$rootScope');
$scope = $rootScope.$new();
var $controller = $injector.get('$controller');
CreateTarget = function() {
$controller('MasterController', {$scope: $scope});
}
});
});
describe('$locationChangeSuccess', function() {
it('should set $scope.success to true', function() {
controller = CreateTarget();
$rootScope.$broadcast('$locationChangeSuccess');
expect($scope.success).toBe(true);
});
});
});
关于angularjs - 如何在 Jasmine 测试中测试 $scope.$on 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24314885/
我是一名优秀的程序员,十分优秀!