I'm using chai-as-promised to test some promises. My issue is I'm not sure how to have multiple expect statements in a single test. In order for the expect().to.be.fulfilled to work properly, I need to return it, like this:

我正在使用chai-as-promise来测试一些承诺。我的问题是我不确定如何在一次测试中使用多个期望语句。为了让expect()。to.be.fulfilled正常工作,我需要返回它,如下所示:

it('test', () => {
  return expect(promise).to.be.fulfilled
}

... or to use notify, like this:

...或使用通知,如下所示:

it('test', (done) => {
  expect(promise).to.be.fulfilled.notify(done)
}

The issue comes when I have another thing I need to check, such as that a certain function gets called, like this:

问题来自于我需要检查的另一件事,例如调用某个函数,如下所示:

it('test', (done) => {
  var promise = doSomething()
  expect(sinon_function_spy.callCount).to.equal(1)
  expect(promise).to.be.fulfilled.notify(done)
})

The problem here is that, because doSomething() is asynchronous, the call to sinon_function_spy may not have occurred yet when I call that expect, making this test flaky. If I use a then, like this:

这里的问题是,因为doSomething()是异步的,所以当我调用期望时,对sinon_function_spy的调用可能还没有发生,使得这个测试变得不稳定。如果我使用a,那么:

it('test', (done) => {
  var promise = doSomething()
  promise.then(() => {
    expect(sinon_function_spy.callCount).to.equal(1)
  })
  expect(promise).to.be.fulfilled.notify(done)
})

Then the test technically passes and fails as expected, but it will fail because the promise gets rejected, due to the thrown exception in the then call. Similarly, if I have a case where the promise is expected to reject:

然后测试在技术上传递并按预期失败,但它将失败,因为promise被拒绝,因为在then调用中抛出异常。同样,如果我有一个承诺预计拒绝的情况:

it('test', (done) => {
  var promise = doSomething()
  promise.then(() => {
    expect(sinon_function_spy.callCount).to.equal(1)
  })
  expect(promise).to.be.rejected.notify(done)
})

Then the check on the sinon_function_spy never gets called, since the promise was rejected and doesn't call then.

然后对sinon_function_spy的检查永远不会被调用,因为承诺被拒绝了,然后没有调用。

How can I get both expect statements to reliably execute and return the correct values?

如何让两个expect语句可靠地执行并返回正确的值?

2 个解决方案

#1


1

In the case of wanting to assert that the Promise is fulfilled and a call was performed as expected, you don't really need that first part as an assertion. The mocha test case itself will fail if the Promise rejects as long as you are returning it:

在想要断言Promise已经完成并且调用按预期执行的情况下,您并不需要将第一部分作为断言。如果Promise拒绝,只要你返回它,mocha测试用例本身就会失败:

it('test', () => {
  return doSomething()
    .then(() => {
      expect(sinon_function_spy.callCount).to.equal(1)
    });
});

If the Promise returned by doSomething() rejects, so will the test case. If the expect assertion fails, it will also fail the test case with that failed assertion. If you want to be a bit more explicit:

如果doSomething()返回的Promise拒绝,那么测试用例也是如此。如果期望断言失败,那么失败的断言也会使测试用例失败。如果你想更明确一点:

it('test', () => {
  return doSomething()
    .then(() => {
      expect(sinon_function_spy.callCount).to.equal(1)
    }, err => {
      expect(err).to.not.exist;
    });
});

...you can catch the error. Note that with this flavor of then with two callbacks, the assertion failing in the first callback will not reach the second callback, so it'll just be Mocha that sees the failed assertion.

...你可以发现错误。请注意,有了两个回调的这种风格,第一个回调中的断言失败将不会到达第二个回调,因此它只是Mocha看到失败的断言。

Here's how you can do an expected failed Promise:

以下是如何做到预期失败的承诺:

it('test', () => {
  return doSomething()
    .then(() => {
      throw new Error('Promise should not have resolved');
    }, err => {
      expect(err).to.exist;
      expect(sinon_function_spy.callCount).to.equal(1)
    });
})

更多相关文章

  1. 如何测试潜在的“浏览器崩溃”JavaScript?
  2. How to learn js properly(week4)使用js建立的动态测试网页
  3. Python PyV8安装测试(Win7)
  4. 如何让django芹菜写入测试数据库进行功能测试?
  5. python 之 自制测试框架
  6. Python测试函数和类 笨方法学习Python
  7. Python 3.4 AssertEqual()在Django单元测试中使用时的未预测行为
  8. Django:测试成功加载静态文件
  9. Appium基于Python APP自动化测试框架

随机推荐

  1. android开发--RelativeLayout用到的一些
  2. Android(安卓)ImageSpan与TextView中的te
  3. Android中WebView如何加载JavaScript脚本
  4. Android预制APP第一次打开时不弹权限提示
  5. AndroidのUI布局之layout weight
  6. android-GooglePlay安装来源追踪PlayInst
  7. Android(安卓)更新UI的两种方法——handl
  8. android 设置时区
  9. Android的XMPP协议的smack开源框架
  10. Android 获取基站信息