Ada*_*lin 10 unit-testing jasmine angularjs
我是一个单元测试控制器,我想测试一个事件处理程序.说我的控制器看起来像:
myModule.controller('MasterController', ['$scope', function($scope){
$scope.$on('$locationChangeSuccess', function() {
$scope.success = true;
});
}]);
Run Code Online (Sandbox Code Playgroud)
我会在我的Jasmine测试中播放吗?我会发射它吗?有接受的标准吗?
Ada*_*lin 16
我想出的解决方案如下:
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);
});
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13665 次 |
| 最近记录: |