本文介绍了预期将调用一个断言,但收到零个断言调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Jest测试方法...该方法应返回Promise.reject()。
I am trying to test a method using Jest... The method should return Promise.reject() .
这是我写的代码:
test('testing Invalid Response Type', () => {
const client = new DataClient();
client.getSomeData().then(response => {
console.log("We got data: "+ response);
}).catch(e => {
console.log("in catch");
expect(e).toBeInstanceOf(IncorrectResponseTypeError);
});
expect.assertions(1);
});
当我运行测试时,它会打印 catch,但失败,但出现以下异常:
预期将调用一个断言,但收到零个断言调用。
When I run the test, it prints "in catch" but fails with this exception: Expected one assertion to be called but received zero assertion calls.
console.log src/data/dataclient.test.js:25
in catch
● testing Invalid Response Type
expect.assertions(1)
Expected one assertion to be called but received zero assertion calls.
at extractExpectedAssertionsErrors (node_modules/expect/build/extract_expected_assertions_errors.js:37:19)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
推荐答案
我通过在块之前添加return语句来解决它。
使用return语句,该函数将等待catch块完成..因此将执行期望。.
I solved it by adding return statement before the block.With return statement the function will wait for catch block to finish.. and hence expect will be executed..
test('testing Invalid Response Type', () => {
const client = new DataClient();
return client.getSomeData().then(response => {
console.log("We got data: "+ response);
}).catch(e => {
console.log("in catch");
expect(e).toBeInstanceOf(IncorrectResponseTypeError);
});
expect.assertions(1);
});
这篇关于预期将调用一个断言,但收到零个断言调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!