首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >随机失败的JS测试

随机失败的JS测试
EN

Stack Overflow用户
提问于 2017-08-11 22:29:34
回答 1查看 58关注 0票数 0

嗨,我在运行我的测试时遇到了这个错误,我已经阅读了承诺并完成了,我仍然不确定在我的测试中把它放在哪里,或者最好在每个之前做一次,而不是为了避免重复?在哪里,什么是实现已完成的承诺的最佳方式?

错误:超过2000ms的超时时间。对于异步测试和钩子,请确保调用了"done()“;如果返回一个Promise,请确保它可以解析。

代码语言:javascript
复制
    const chakram = require('chakram');
const expect = chakram.expect;


describe("Json assertions", function () {
    it("Should return the matching test file and have a 200 response", function () {

        let expected = require('../../test/fixtures/testfile.json');
        let response = chakram.get("http://test");
        expect(response).to.have.json(expected);
        expect(response).to.have.status(200);
        return chakram.wait();
    });
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-11 22:47:53

我不熟悉chakram,但通常有一种模式可以测试您的承诺(使用done):

代码语言:javascript
复制
describe('Test Suite', function() {
  it('should do something...', function(done) {
    const testValue = 'testValue';
    someAsyncFunction(testValue).then(function(result) {
      result.should.have.property('foo');
      result.should.have.property('bar');
      done();
    });
  ]);
});

现在,就您所拥有的内容而言,docs for Chakram似乎展示了如何使用promises进行测试(在Promises标题下)。所以你改编的代码应该是这样的:

代码语言:javascript
复制
const chakram = require('chakram');
const expect = chakram.expect;

describe("Json assertions", function () {
  it("should...", function () {
    let expected = require('../../test/fixtures/testfile.json');
    chakram.get("http://test").then(function(response) {
      expect(response).to.have.json(expected);
      expect(response).to.have.status(200);
    });
  });
});

再说一次,我不知道那个库,但是如果你的测试运行人员仍然抱怨done,像这样添加done

代码语言:javascript
复制
describe("Json assertions", function () {
  it("should...", function (done) {
    let expected = require('../../test/fixtures/testfile.json');
    chakram.get("http://test").then(function(response) {
      expect(response).to.have.json(expected);
      expect(response).to.have.status(200);
      done();
    });
  });
});

编辑:donedescribe块中,应该在it中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45637674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档