AngularJS单元测试element.bind

时间:2015-12-22 10:06:23

标签: angularjs unit-testing angularjs-directive karma-jasmine

在我directive召唤的data-type-ahead内,我在以下一系列事件中有以下内容:

$scope.test = 5;

// Bind blur event and clear the value of 
element.bind('blur', function(){
    $scope.test = 0;
});

我已尝试在单元测试中使用大量内容来正确测试此blur事件的功能但是我没有成功。我已经看到了函数triggerHandler的提及。以下是我对单元测试的尝试:

//Inject $compile and $rootScope before each test.
beforeEach(inject(function(_$compile_, _$rootScope_) {
    $compile = _$compile_;
    $rootScope = _$rootScope_;

    $scope = $rootScope.$new();
    $scope.test = 5

    html = angular.element('<input type="text" data-type-ahead/>');

    //Apply $scope to directive html.
    directive = $compile(html)($scope);

    //Trigger digest cycle.
    $scope.$digest();
}));

it('should trigger the events bound to the blur event on the directive', function() {
    html.triggerHandler('blur')
    expect($scope.test).toEqual(0);
});

然而这是失败的,因为$scope.test仍然存在于5.是否html元素不正确,在触发事件后是否需要另一个$digest$apply

0 个答案:

没有答案