javascript - 开玩笑期望使用不包含特定字段的对象调用模拟
问题描述
我想验证是否使用不包含特定字段的对象调用了模拟 API。
expect(api.method).toBeCalledWith(expectedObject);
由于api.method
调用的实际参数可以是该测试的子集,因此如果包含更多字段(其中可能是特定字段) ,expectedObject
该测试也会通过。actualObject
如何以某种方式重写测试,如果actualObject
不等于,则测试失败expectedObject
?
解决方案
你可以试试这样的
// 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);
推荐阅读
- linux - 组播和集群
- python - 用 x 值范围填充 matplotlib 上的 x 和 y 值
- docker - 通过 Jenkinsfile 在 docker 主机上限制并发构建
- ruby-on-rails - 如何在不使用 Javascript 的情况下删除用户帐户?
- php - 在遍历 Drupal 8 EntityQuery 结果时达到 PHP 内存限制。我该如何保持下去?
- html - 在 HTML5 模板中嵌入 Youtube
- python - tkinter 对话框突出显示父条目文本
- python - 在自定义 Jupyterhub 身份验证器中设置 spawner 环境变量
- ibm-mobilefirst - 如何从 MFP 8.0 中的 java 适配器异步调用 javascript 适配器?
- php - 如何在 PHP 中解压缩之前在 JavaScript 中压缩的数据