【问题标题】:Is it possible to test a sequence of asynchronous function calls with D.O.H是否可以使用 D.O.H 测试一系列异步函数调用
【发布时间】:2009-06-17 13:51:03
【问题描述】:

我正在尝试使用doh.Deferred 编写一个测试来检查以下事件序列:

  1. 使用用户 A 登录(异步)
  2. 注销(同步)
  3. 使用用户 A 登录(异步)

第二个回调函数的返回值是另一个doh.Deferred 对象。我的印象是 d 的回调链会等待 d2 但它不会。测试在 d2.callback 被调用之前完成。

我哪里错了?

有人知道我测试这种行为的更好方法吗?

function test() {
    var d = new doh.Deferred();

    d.addCallback(function() {  
        Comm.logout(); /* synchronus */
        try {   
            // check with doh.t and doh.is
            return true;
        } catch (e) {
            d.errback(e);
        }
    });

    d.addCallback(function() {
        var d2 = new dojo.Deferred();
        /* asynchronus - third parameter is a callback */
        Comm.login('alex', 'asdf', function(result, msg) {
                try {
                    // check with doh.t and doh.is
                    d2.callback(true);
                } catch (e) {
                    d2.errback(e);
                }                   
            });
        return d2; // returning doh.Defferred -- expect d to wait for d2.callback
    });     

    /* asynchronus - third parameter is a callback */
    Comm.login('larry', '123', function (result, msg) {
        try {
            // check with doh.t and doh.is 
            d.callback(true);
        } catch (e) {
            d.errback(e);
        }
    }); 

    return d;
}

【问题讨论】:

    标签: javascript testing dojo doh


    【解决方案1】:

    这行得通。 d2 的范围是问题所在。

    function test() {
        var d = new doh.Deferred();
        var d2 = new doh.Deferred();
    
        d.addCallback(function() {  
            Comm.logout(); /* synchronus */
            try {   
                    // check with doh.t and doh.is
                    return true;
            } catch (e) {
                    d.errback(e);
            }
        });
    
        d.addCallback(function() {
            /* asynchronus - third parameter is a callback */
            Comm.login('alex', 'asdf', function(result, msg) {
                            try {
                                    // check with doh.t and doh.is
                                    d2.callback(true);
                            } catch (e) {
                                    d2.errback(e);
                            }                                       
                    });
            return d2; // returning doh.Deferred -- waits for d2.callback
        });         
    
        /* asynchronus - third parameter is a callback */
        Comm.login('larry', '123', function (result, msg) {
            try {
                    // check with doh.t and doh.is 
                    d.callback(true);
            } catch (e) {
                    d.errback(e);
            }
        }); 
    
        return d;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-04-04
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      • 2018-10-02
      • 1970-01-01
      相关资源
      最近更新 更多