2016-01-29 43 views
2

我有以下的茉莉花测试,试图发射一个功能onInput,但它根本没有激发它。消防功能onInput - 茉莉花测试

问题

使用夹具,可我,我会在标准的HTML使用,并期望它找到包含的脚本功能?

这就是我想:

// check an input cannot accept more than x characters 
describe("when the page is loaded", function(){ 

    var fixture; 

    beforeEach(function() { 

     fixture += "<input id='textInput' onInput='limitLength2(this,2)'>"; 
     setFixtures(fixture); 

    }); 

    it("an input should only allow x number of characters", function(){ 

     $('#textInput').trigger("focus").val($('#textInput').val() + '1'); 
     $('#textInput').trigger("focus").val($('#textInput').val() + '1'); 
     $('#textInput').trigger("focus").val($('#textInput').val() + '1'); 

     expect($('#textInput').val().length).toEqual(2); 

    }); 

    afterEach(function() { 
     fixture = ""; 
     fixture = null; 
    }); 

}) 

测试失败像Expected 3 to equal 2.

我还添加了console.log()limitLength2(),可以确认,这不是解雇。

这可以这样做吗?

回答

1

如果您使用jQuery更改值,则'onchange'事件不会触发,'oninput'也不会触发。

您需要.trigger('input').val(.... + 1)后,然后它应该按预期工作:-)

+0

神该死的,我试过很多的组合,但不是一个 - 非常感谢!当我可以接受时 –

+0

@DarrenSweeney有史以来最好的回复;-)通常情况下它是这样的小事情,当你发现时它确实会让你疯狂!很高兴我有帮助:-) – DoXicK