测试未用Jest调用的回调? [英] Testing callbacks that are not called with Jest?

查看:245
本文介绍了测试未用Jest调用的回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以测试是否未使用Jest调用回调?

Is there a way to test that a callback is not called with Jest?

例如:

o.subscribe(cb, cbError, cbComplete);

应触发cbcbComplete回调,而不应触发cbError回调.

The cb and cbComplete callbacks should fire and the cbError callback should not fire.

有没有一种方法可以测试cbError是否从未被调用?

is there a way to test that cbError is never called?

推荐答案

每个@Richard的评论:

Per @Richard's comment:

let error = false;
let cbError = ()=> { error =true };
let cbComplete = ()=>{
  complete = true;
  expect(complete).toBeTruthy();
  expect(error).toBeFalsy();
  done(); //This is the async callback that Jest provides to let Jest know that the test is done.

}

在完整的回调中,我们测试错误仍然是false.由于Observable完成后为false,因此从未调用cbError回调,因为该回调与其他回调是互斥的.

Inside the complete callback we test that error is still false. Since it's false after the Observable completes, the cbError callback was never called, because that callback is mutually exclusive from the other callbacks.

此类指示未调用cbError.通过设计cbError和cbComplete回调是互斥的,但是我们不能在两个地方都开玩笑地调用done(),因为它在测试中等于竞争条件,因此从本质上讲,我们必须信任设计在这种情况下.如果有人对此有其他想法,请发表评论.

This sort of gives an indication that the cbError is not called. By design the cbError and cbComplete callbacks are meant to be mutually exclusive, but we can't call done() from jest in both places, because it would equal a race condition in the test, so it that essentially we have to trust the design in this case. If anyone has any other thoughts on this please leave a comment.

这篇关于测试未用Jest调用的回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆