2016-04-18 46 views
0

工作,這是我正在運行茉莉花點擊觸發不會對<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> 

但是,當我用div,span或p替換標記時,測試通過。

<div class="modal-link">Test Link</div> 

是否有某些原因在我的測試中標記不可點擊?在19/04

編輯: 我用噶運行測試,這裏的配置文件

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} 
    ], 

    }); 
}; 
+0

你在使用瀏覽器在你的測試? – MBielski

+0

jasmine默認不瞭解jquery方法,你使用jq jasmine庫嗎? –

+0

@MBielski,我試過Chrome,Firefox和PhantomJS,但測試一直失敗。 –

回答

0

你配置的目標是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); 
} 
+0

我想使用這個,但我得到'el.dispatchEvent不是一個函數',任何提示? – teone

+0

這意味着您的目標元素未找到。如果你逐步瞭解它,你會發現'el'沒有被定義。 – MBielski