简体   繁体   English

在测试JavaScript时,还有比setTimeout更好的方法来等待asnyc回调吗?

[英]Any better way than setTimeout to wait for asnyc callbacks when testing JavaScript?

Given this code: 鉴于此代码:

showForm = function (url) {
        return $.get(url, function (html) {
             $('body').append();              
        });
 };

When using sinon.js , jQuery.mockjax.js and expect.js , I have the following passing test: 当使用sinon.jsjQuery.mockjax.jsexpect.js ,我有以下传递测试:

it("showForm calls jQuery.append", function () {

    $.mockjax({
          url: '/fake'                    
    });

    var spy = sinon.spy($.fn, "append");             
    presenter.showForm('/fake');

    setTimeout(function () { 
        expect(spy.called).to.be.equal(true);
    }, 1000);
});

Using the setTimeout function to wait on the callback from the asynchronous $.get smells bad and it will slow down my test suite if I do too much of it. 使用setTimeout函数等待来自异步$.get的回调气味很糟糕,如果我做了太多的话,它会减慢我的测试套件的速度。

However, I feel that the intent is reasonably clear and it does seem to test exactly what I want. 但是,我觉得意图是相当清楚的,它似乎确实测试了我想要的。

Is there a better way and can you please explain what's going on in your answer? 有没有更好的方法,请您解释一下您的答案中发生了什么?

Pass your anonymous function to show form as a parameter and have it called in the callback after the append. 传递您的匿名函数以将表单显示为参数,并在追加后在回调中调用它。 To ensure its called you could make it an $.ajax request that covers the error callback as well. 为了确保它被调用,你可以使它成为一个$ .ajax请求,它也包含错误回调。

This way it is called exactly when the get request finishes with no excess time or called too early. 这样,当get请求完成且没有多余时间或过早调用时,就会调用它。

showForm = function (url, callback) {
        return $.get(url, function (html) {
             $('body').append();   
             if(callback != undefined)
             callback();
        });
 };

it("showForm calls jQuery.append", function () {

    $.mockjax({
          url: '/fake'                    
    });

    var spy = sinon.spy($.fn, "append");

    presenter.showForm('/fake', function (){ 
        expect(spy.called).to.be.equal(true);
    });
});

You should use sinons fakeServer . 你应该使用sinons fakeServer This will call your ajax callback immediately, when the $.ajax call was made. $.ajax调用完成后,这将立即调用您的ajax回调。 See this jsFiddle 看到这个jsFiddle

before(function(){
  var server = sinon.fakeServer.create();
  server.respondWith('response');
})

it("showForm calls jQuery.append", function () {
    var spy = sinon.spy($.fn, "append");             
    presenter.showForm('/fake');
    server.respond();
    expect(spy.called).to.be.equal(true);
});

Use $.doTimeout plugin 使用$ .doTimeout插件

Check the following documentation. 请查看以下文档。

http://benalman.com/projects/jquery-dotimeout-plugin/ http://benalman.com/projects/jquery-dotimeout-plugin/

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM