gpt4 book ai didi

angularjs - AngularJS 上的单元测试 $scope.$

转载 作者:行者123 更新时间:2023-12-02 23:59:28 26 4
gpt4 key购买 nike

大家好,我正在努力测试 $.on 函数,我正在寻找对此的任何建议或帮助:

Controller

  $scope.$on("sampleFilesSelected", function(event, args) {
$scope.sampleFiles = args.newSampleFiles;
});

规范

describe('Testing a $.on', function() {
var $scope = null;
var ctrl = null;


beforeEach(module('test'));

it('should invoke myEvent when "myEvent" broadcasted', inject(function($rootScope, $controller) {
$scope = $rootScope.$new();

ctrl = $controller('MainCtrl', {
$scope: $scope
});

$scope.$broadcast('myEvent');
expect($scope.sampleFilesSelected).toBe(true);
}));
});

错误

TypeError: Unable to get property 'newSampleFiles' of undefined or null reference
undefined

最佳答案

您应该向您的事件传递一个值,在断言之前调用 $digest :

$scope.$broadcast('myEvent', { 'newSampleFiles' : true } );
$scope.$digest();
expect($scope.sampleFilesSelected).toBe(true);

关于angularjs - AngularJS 上的单元测试 $scope.$,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35179797/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com