Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用QUnit 2.x assert.async()测试ajax调用_Javascript_Jquery_Ajax_Unit Testing_Qunit - Fatal编程技术网

Javascript 使用QUnit 2.x assert.async()测试ajax调用

Javascript 使用QUnit 2.x assert.async()测试ajax调用,javascript,jquery,ajax,unit-testing,qunit,Javascript,Jquery,Ajax,Unit Testing,Qunit,我正在使用Qunit2.0.1对现有代码库进行单元测试。我有一个函数,它进行ajax调用,然后启用一个按钮并设置一个布尔值。我正在尝试编写一个QUnit测试,以断言在调用函数后按钮已启用,布尔值为false 我的ajax调用函数: self.saveComment = function() { if(self.isEditing()) { $('#saveCommentButton').attr('disabled', 'disabled');

我正在使用Qunit2.0.1对现有代码库进行单元测试。我有一个函数,它进行ajax调用,然后启用一个按钮并设置一个布尔值。我正在尝试编写一个QUnit测试,以断言在调用函数后按钮已启用,布尔值为false

我的ajax调用函数:

self.saveComment = function() {     
    if(self.isEditing())
    {   
        $('#saveCommentButton').attr('disabled', 'disabled');

        var comment = self.newComment();            
        var data = {
                newCommentText : comment,
                objectId: objectId,
                objectType : objectTypeId
        };

        $.ajax({
            type: "POST",
            url: "addBOComment.action",
            data: data,
            success: function(comments){
                self.comments(comments);                    
                $('#saveCommentButton').attr('enabled', 'enabled');
                self.isEditing(false);                  
            }
        });         
    }
};
我的QUnit测试:

QUnit.test("test saveComment() - async", function(assert){      
    $('#qunit-fixture').append('<input type="button" id="saveCommentButton" disabled="disabled"/>')

    model.isEditing(true);
    model.newComment("qunit test for comments");

    var done = assert.async();

    window.setTimeout(function() {
     model.saveComment(); //function with ajax call
     assert.equal(model.isEditing(), false, 'success');
     assert.equal($('#saveCommentButton').attr("enabled"), "endabled", 'success');  
     done();

   }, 150);

});
QUnit.test(“test saveComment()-async”,函数(assert){
$('#qunit fixture')。附加('')
模型.i编辑(真);
新来的模型(“评论的qunit测试”);
var done=assert.async();
setTimeout(函数(){
model.saveComment();//带有ajax调用的函数
assert.equal(model.isEditing(),false,“success”);
assert.equal($('#saveCommentButton')).attr(“已启用”)、“可终止”和“成功”);
完成();
}, 150);
});

测试将运行,但从未调用success,因此我的按钮被禁用,布尔值仍然为true。我做错了什么?

找到解决我问题的方法:找到解决我问题的方法: