大家好,我正在努力测试 $.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/