我正在嘗試測試我正在寫入的新指令。但是,我似乎無法獲得在Karma/Jasmine中使用jQuery觸發的keydown事件。AngularJS中的jQuery觸發器事件Karma測試
下面是測試的簡化版本:
'use strict';
describe('', function() {
var $compile;
var $scope;
beforeEach(inject(function(_$compile_, _$rootScope_) {
$compile = _$compile_;
$scope = _$rootScope_.$new();
}));
describe('Getting Trigger To Work', function() {
it('Should Trigger a KeyDown Event', function() {
var el = $compile('<form name="testing"><input id="field1" name="testfield" ng-model="result" type="text" ng-minlength="5" ng-maxlength="50"/></form>')($scope);
console.log(el);
var inputEl = el.find('input');
console.log(inputEl);
var e = $.Event('keydown');
e.which = 65;
inputEl.trigger(e);
});
});
});
我的人緣配置包括AngularJS和jQuery。下面是部分:
//
files: [
'lib/angular.js',
'lib/angular-mocks.js',
'lib/jquery-1.10.2.min.js',
'test/**/*Spec.js'
],
當我運行測試,我得到我的輸入元素沒有觸發方法的錯誤:
INFO [watcher]: Changed file "/Volumes/Macintosh HD/dev_libraries/val-on-timeout/test/valOnTimeoutSpec.js".
LOG: Object{0: <form name="testing" class="ng-scope ng-pristine ng-valid"><input id="field1" name="testfield" ng-model="result" type="text" ng-minlength="5" ng-maxlength="50" class="ng-pristine ng-valid"></form>, length: 1}
LOG: Object{0: <input id="field1" name="testfield" ng-model="result" type="text" ng-minlength="5" ng-maxlength="50" class="ng-pristine ng-valid">, length: 1}
Chrome 32.0.1700 (Mac OS X 10.8.5) testing valOnTimeout Should load FAILED
TypeError: Object [object Object] has no method 'trigger'
at null.<anonymous> (/Volumes/Macintosh HD/dev_libraries/val-on-timeout/test/valOnTimeoutSpec.js:79:21)
Chrome 32.0.1700 (Mac OS X 10.8.5): Executed 1 of 1 (1 FAILED) ERROR (0.327 secs/0.033 secs)
起初,你可能會認爲jQuery是不包括在內。但是,它包含在內。如果不是,測試將失敗,在$.Event
。
關於讓keydown事件處理輸入的建議?
這就是問題所在。 '[object Object]沒有方法'trigger''所以我不能觸發任何事件。 – Justin
對不起,我的意思是'angular.element(inputEl).trigger('keydown')' – dcodesmith
@dcodesmith謝謝。試過。沒有運氣。仍然'對象[對象對象]沒有方法'trigger'' – Justin