首页 > 解决方案 > 开玩笑期望使用不包含特定字段的对象调用模拟

问题描述

我想验证是否使用不包含特定字段的对象调用了模拟 API。

expect(api.method).toBeCalledWith(expectedObject);

由于api.method调用的实际参数可以是该测试的子集,因此如果包含更多字段(其中可能是特定字段) ,expectedObject该测试也会通过。actualObject

如何以某种方式重写测试,如果actualObject不等于,则测试失败expectedObject

标签: javascriptunit-testingjestjs

解决方案


你可以试试这样的

// get the first call to your method
const method = api.method.mock.calls[0]; 

//check if you have expectedObject as a subset of the arguments
expect(method[0]).toMatchObject(expectedObject); 

//key is the field that shouldn't be part of the arguments
expect(method[0].has(key)).toEqual(false); 

推荐阅读