Unit testing 使用Jest酶的异步/等待测试

Unit testing 使用Jest酶的异步/等待测试,unit-testing,async-await,jestjs,Unit Testing,Async Await,Jestjs,正在尝试使用Jest和Ezyme为以下代码运行测试。代码实际上正在传递,但不影响其覆盖范围。只是想知道我可以添加什么,以便正常工作并增加测试覆盖率 以下是函数: async getCurrencies() { const { user, services } = this.props; let types = response.body ? response.body.value : null; let response = await DropdownModels.getCurre

正在尝试使用Jest和Ezyme为以下代码运行测试。代码实际上正在传递,但不影响其覆盖范围。只是想知道我可以添加什么,以便正常工作并增加测试覆盖率

以下是函数:

async getCurrencies() {
  const { user, services } = this.props;
  let types = response.body ? response.body.value : null;
  let response = await DropdownModels.getCurrencies({ user, services })
  let temp = types.map((type) => {        
    return {
     label: type.Name,
     value: type.Currency1,
   }
  })
 this.setState({ CurrencyOptions: temp });
}
以下是我的测试用例:

it ('Test getCurrencies function ',async() => {
 wrapper.setProps({
    user:{},
    serviceS:{},
  })
 wrapper.find('TransactionForm').setState({
    CurrencyOptions:[[]]
   });
 wrapper.update();
 await expect(wrapper.find('TransactionForm').instance().getCurrencies('test')).toBeDefined();
});
还尝试了以下方法

const spy = jest.spyOn(wrapper.find('TransactionForm').instance(), 'getCurrencies');
await expect(spy).toBeCalled()
但是使用spy获得以下错误:

   expect(jest.fn()).toBeCalled()
   Expected mock function to have been called.

首先,让我们从编写测试的基本概念开始

我应该测试什么?

所有的代码行都应该经过测试,以达到尽可能高的覆盖率——当然是100%。另一方面,该百分比在某些情况下可能不可靠

我为什么要处理测试?

测试有助于确定以前的实现是否因新功能而中断。您只需“按下按钮”,而不是手动验证

在基础和高层次上还有许多其他概念,但是让我们尽量保持这个列表的简短,然后再回到问题的细节


为了坚持上面的例子,让我留下一些观察

it('testgetcurrences函数',async()=>{…});
这个测试描述并没有说明这个案例的意图。它测试功能,但如何测试?该功能包括哪些部分?如果描述中没有具体细节,回答这些问题真的很困难

const spy=jest.spyOn(wrapper.find('TransactionForm').instance(),'getCurrencies');
我不确定什么是
TransactionForm
,但根据,它接受以下参数:

jest.spyOn(对象,方法名)
您确定
wrapper.find('TransactionForm').instance()返回一个对象,并且还包含
getCurrences
函数吗

let response=wait-DropdownModels.getCurrencies({user,services});
这很奇怪,也很令人困惑,
DropdownModels
还有一个
getcurrences
方法。这可能不是问题,但我宁可考虑重命名它。


最后,以下是一些示例测试用例,它们可能是您案例的良好起点:

description(“GetCurrences”),()=>{
它(“使用'user'和'services'道具调用'DropdownModels.getcurrences',()=>{
//别忘了在样品中设置所需的道具。
//只需用jest.spyOn模拟'DropdownModels.getCurrencies'。
});
它(“将'temp'设置为状态”,()=>{
//测试预期数据是否设置为状态。
});
});