【问题标题】:Async Test in Jasmine 2.6Jasmine 2.6 中的异步测试
【发布时间】:2017-07-12 17:23:24
【问题描述】:

自 2.x 以来,异步测试的语法发生了变化,documentation 不清楚。

有人可以澄清我如何执行一些代码,阻塞 3 秒,然后使用新语法运行测试条件吗?

it('should update the table when new data is provided', function() {
  var newData = ",0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23\nX-Y,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1";

  fixture.datum(csv).call(fp);

  expect(fp.dataset()).toEqual(csv);

  fp.dataset(newData);

  expect(fp.dataset()).toEqual(newData);

  //block for 3 seconds
  expect(fixture.selectAll(".row").nodes().length).toBe(3);

});

【问题讨论】:

    标签: unit-testing jasmine bdd jasmine2.0


    【解决方案1】:

    done 需要作为参数传递给规范,done() 需要作为 setTimeout() 阻止。

    如果异步规范总共超过 5 秒,它将失败,请参阅 jasmine 文档摘录了解更多信息:

    默认情况下,jasmine 将等待 5 秒以等待异步规范完成,然后再导致超时失败。如果在调用 done 之前超时过期,则当前规范将被标记为失败,并且套件执行将继续执行,就像调用 done 一样。

    如果特定规范应该更快地失败或需要更多时间,这可以通过向其传递超时值等方式进行调整。

    it('should update the table when new data is provided', function(done) {
      var newData = ",0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23\nX-Y,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1";
    
      fixture.datum(csv).call(fp);
    
      expect(fp.dataset()).toEqual(csv);
    
      fp.dataset(newData);
    
      expect(fp.dataset()).toEqual(newData);
    
      //block for 3 seconds, then execute expect
      setTimeout(function() {
          expect(fixture.selectAll(".row").nodes().length).toBe(3);
          done(); //dont forget!!
      }, 3000);
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-03
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-28
      相关资源
      最近更新 更多