茉莉触发器按钮事件

Jasmine trigger Button Event

本文关键字:事件 按钮 触发器      更新时间:2023-09-26

我有下面的点击处理程序包装在一个类

var TestUI = function (chatClient, document, elements) {
TestUI.prototype = {
    applyListenersToElements: function () {
        var self = this;
        this.elements.myButton.click(function () {
            self.myClient.postData(data).then(function (response) {
                //all's good
                if(response)
                {
                    ....do something
                }
            }, function (err) {
                console.log("Error " + err);
            });
        });
    }       
}

如何编写Jasmine测试来模拟/执行实际的单击事件,就像在浏览器中单击一样?如何模拟对象和事件?我是否必须在模拟中提供一个HTML片段,或者我可以使用实际文件中的HTML,也许使用jasmine.getFixtures()

刚才看到这个似乎回答了我的问题。

https://luizfar.wordpress.com/2011/01/10/testing-events-on-jquery-objects-with-jasmine/

我可以从使用jasmine.getFixtures()的外部文件中读取HTML。fixturesPath = 'my/new/path';

From https://github.com/velesin/jasmine-jquery.

这个也可以

http://codepen.io/jweden/pen/Irmil

希望对大家有所帮助