2017-09-16 74 views
0

我想测试使用玩笑......这个方法应该返回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); 

    }); 

当我运行测试,它打印“渔获”,但失败与此异常: 预计一个断言被称为却收到零个断言电话。

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) 
+0

看起来你是不是具有匹配的错误类型。 (你发现了一个错误,但它不是'IncorrectResponseTypeError'类型) –

+0

也许你可以尝试在控制台中记录'e'来查看它实际上是什么类型 –

回答

0

我通过在块之前添加return语句来解决它。 随着return语句,函数将等待catch块完成..因此预计将被执行..

client.getSomeData().then(response => { 
      console.log("We got data: "+ response); 
     }).catch(e => { 
      console.log("in catch"); 
      expect(e).toBeInstanceOf(IncorrectResponseTypeError); 

     }); 
     expect.assertions(1);