【问题标题】:Completing Promises after runs/waitsFor in Jasmine 1.3在 Jasmine 1.3 中运行/等待后完成 Promise
【发布时间】:2017-01-06 20:58:13
【问题描述】:

我正在使用 jasmine-node 1.14.5,它在下面使用 jasmine 1.3,我在让 runs/waitFor 与 Promises 正常工作时遇到问题。

在某些测试中,我想runs/waitFor 等待特定条件发生,当它发生时,履行我返回的承诺。但是,当我尝试构造一个传入 function(success, fail) 参数的 Promise 时,runs/waitFor 内的任何代码都不会被调用。但是,如果直接解决了 Promise,它就可以工作。知道前一个选项不起作用吗?

举一些例子,以下工作正常:

  it("should support async execution of test preparation and expectations", function(done) {
    var p = Promise.resolve("boo")
      .then(function() {
        var p2 = Promise.resolve("whatever");

        runs(function() {
          flag = false;
          value = 0;
          intId = setInterval(function() {
            console.log(value);
            if (++value == 3) { clearInterval(intId); flag = true; }
          }, 500);
        });

        waitsFor(function() {
          return flag;
        }, "The Value should be incremented", 5000);

        runs(function() {
          expect(value).toEqual(3);
        });

        return p2;
      });
    p.then(function() {
      done();
    }).catch(function(err) {
      done(err);
    });
  });

但另一方面,这不起作用,因为虽然runs/waitsFor 被调用没有问题,但内部的回调却没有:

  it("should support async execution of test preparation and expectations", function(done) {
    var p = Promise.resolve("boo")
      .then(function() {
        return new Promise(function (fulfil, reject) {
          runs(function () {
            flag = false;
            value = 0;
            intId = setInterval(function () {
              console.log(value);
              if (++value == 3) {
                clearInterval(intId);
                flag = true;
              }
            }, 500);
          });

          waitsFor(function () {
            return flag;
          }, "The Value should be incremented", 5000);

          runs(function () {
            expect(value).toEqual(3);
            fulfil();
          });
        });
      });
    p.then(function() {
      done();
    }).catch(function(err) {
      done(err);
    });
  });

我也尝试了以下方法,但也不起作用,它的行为与前面的示例相同:

  it("should support async execution of test preparation and expectations", function(done) {
    var p = Promise.resolve("boo")
      .then(function() {
        var outerFulfil;
        var outerReject;
        var p2 = new Promise(function(fulfil, reject) {
          outerFulfil = fulfil;
          outerReject = reject;
        });

        runs(function() {
          flag = false;
          value = 0;
          intId = setInterval(function() {
            console.log(value);
            if (++value == 3) { clearInterval(intId); flag = true; }
          }, 500);
        });

        waitsFor(function() {
          return flag;
        }, "The Value should be incremented", 5000);

        runs(function() {
          expect(value).toEqual(3);
          outerFulfil();
        });

        return p2;
      });
    p.then(function() {
      done();
    }).catch(function(err) {
      done(err);
    });
  });

知道如何解决吗?尽管第一个示例有效,但它的行为方式并不符合我的要求,因为我只希望在执行 waitsFor 之后的断言后履行承诺。

干杯, 高德

【问题讨论】:

    标签: javascript jasmine jasmine-node


    【解决方案1】:

    我最终完全放弃了运行/等待,而是执行基于时间的递归 Promise 循环,例如

    function waitUntil(expectF, cond, op) {
      var now = new Date().getTime();
    
      function done(actual) {
        return cond(actual)
          && new Date().getTime() < now + MAX_WAIT;
      }
    
      function loop(promise) {
        exports.sleepFor(100); // brief sleep
    
        // Simple recursive loop until condition has been met
        return promise
          .then(function(response) {
            return !done(response)
              ? loop(op())
              : response;
          })
          .catch(function() {
            return loop(op());
          });
      }
    
      return loop(op())
        .then(function(actual) {
          expectF(actual);
        });
    }
    

    op 是一个返回 Promise 的操作。

    例如,我想等到一定数量的节点加入集群:

    function waitUntilView(expectNumMembers, nodeName) {
      return waitUntil(
        function(members) { expect(members.length).toEqual(expectNumMembers); },
        function(members) { return _.isEqual(expectNumMembers, members.length); },
        getClusterMembers(nodeName)
      );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 2019-03-31
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 2016-09-15
      相关资源
      最近更新 更多