Jasmine 点击触发器在 <a> 上不起作用
Jasmine click trigger doesn't work on <a>
这是我的规格 运行宁
describe("Test", function() {
beforeEach(function(){
loadFixtures('modal.html');
});
it ("click a tag", function(){
var spy = spyOnEvent($('.modal-link'), 'click');
$('.modal-link').trigger('click');
expect('click').toHaveBeenTriggeredOn($('.modal-link'));
expect(spy).toHaveBeenTriggered();
});
});
夹具内部(modal.html),看起来像这样
<a class="modal-link">Test Link</a>
测试失败并出现此错误:
Expected event [object Object] to have been triggered on [object Object]at Object.<anonymous>
Expected event click to have been triggered on [object Object] at Object.<anonymous>
但是当我将a标签替换为div、span或p时,测试通过了。
<div class="modal-link">Test Link</div>
在我的测试中 a 标签不可点击的原因是什么?
2004 年 19 月编辑:
我使用 Karma 运行 进行测试,这是配置文件
module.exports = function(config) {
config.set({
frameworks: ['jquery-1.11.0', 'jasmine-jquery', 'jasmine'],
reporters: ['mocha'],
browsers: ['PhantomJS'],
plugins: [
// Karma will require() these plugins
'karma-jasmine',
'karma-jasmine-jquery',
'karma-jquery',
'karma-phantomjs-launcher',
'karma-mocha-reporter'
],
preprocessors: {
'test/spec/**/*.html': ['html2js']
},
autoWatch: true,
files: [
{pattern: 'app/libs/jquery.min.js', watched: true, included: true, served: true},
{pattern: 'app/libs/bootstrap/js/bootstrap.js', watched: true, included: true, served: true},
{pattern: 'app/js/myjavascript.js', watched: true, included: true, served: true},
{pattern: 'test/spec/**/*.js', watched: true, included: true, served: true},
{pattern: 'test/spec/**/*.html', watched: true, included: false, served: true}
],
});
};
您的配置针对的是 PhantomJS,它在点击内容时出现问题。完整的答案在这里:How to ng-click an A directive in a PhantomJS test 但简而言之就是在你的测试中使用这个函数:
//Need to create a cross browser click() function no .click() in PhantomJS
function click(el){
var ev = document.createEvent('MouseEvent');
ev.initMouseEvent(
'click',
true /* bubble */, true /* cancelable */,
window, null,
0, 0, 0, 0, /* coordinates */
false, false, false, false, /* modifier keys */
0 /*left*/, null
);
el.dispatchEvent(ev);
}
这是我的规格 运行宁
describe("Test", function() {
beforeEach(function(){
loadFixtures('modal.html');
});
it ("click a tag", function(){
var spy = spyOnEvent($('.modal-link'), 'click');
$('.modal-link').trigger('click');
expect('click').toHaveBeenTriggeredOn($('.modal-link'));
expect(spy).toHaveBeenTriggered();
});
});
夹具内部(modal.html),看起来像这样
<a class="modal-link">Test Link</a>
测试失败并出现此错误:
Expected event [object Object] to have been triggered on [object Object]at Object.<anonymous>
Expected event click to have been triggered on [object Object] at Object.<anonymous>
但是当我将a标签替换为div、span或p时,测试通过了。
<div class="modal-link">Test Link</div>
在我的测试中 a 标签不可点击的原因是什么?
2004 年 19 月编辑: 我使用 Karma 运行 进行测试,这是配置文件
module.exports = function(config) {
config.set({
frameworks: ['jquery-1.11.0', 'jasmine-jquery', 'jasmine'],
reporters: ['mocha'],
browsers: ['PhantomJS'],
plugins: [
// Karma will require() these plugins
'karma-jasmine',
'karma-jasmine-jquery',
'karma-jquery',
'karma-phantomjs-launcher',
'karma-mocha-reporter'
],
preprocessors: {
'test/spec/**/*.html': ['html2js']
},
autoWatch: true,
files: [
{pattern: 'app/libs/jquery.min.js', watched: true, included: true, served: true},
{pattern: 'app/libs/bootstrap/js/bootstrap.js', watched: true, included: true, served: true},
{pattern: 'app/js/myjavascript.js', watched: true, included: true, served: true},
{pattern: 'test/spec/**/*.js', watched: true, included: true, served: true},
{pattern: 'test/spec/**/*.html', watched: true, included: false, served: true}
],
});
};
您的配置针对的是 PhantomJS,它在点击内容时出现问题。完整的答案在这里:How to ng-click an A directive in a PhantomJS test 但简而言之就是在你的测试中使用这个函数:
//Need to create a cross browser click() function no .click() in PhantomJS
function click(el){
var ev = document.createEvent('MouseEvent');
ev.initMouseEvent(
'click',
true /* bubble */, true /* cancelable */,
window, null,
0, 0, 0, 0, /* coordinates */
false, false, false, false, /* modifier keys */
0 /*left*/, null
);
el.dispatchEvent(ev);
}