unit-testing - 如何在玩笑中检查包含某些键的对象?
问题描述
我很困惑。我刚刚开始开玩笑地编写测试,但我无法弄清楚发生了什么。这是我的输出:
Expected value to equal:
ObjectContaining {"field": "f1", "module": "m1", "rights": {"read": true}}
Received:
{"_id": "5ae85cd0bc5ad3569bf66df8", "field": "f1", "module": "m1", "rights": {"read": true}}
Difference:
- Expected
+ Received
@@ -1,6 +1,7 @@
- ObjectContaining {
+ Object {
+ "_id": "5ae85cd0bc5ad3569bf66df8",
"field": "f1",
"module": "m1",
"rights": Object {
"read": true,
},
183 | ]),
184 | );
> 185 | expect(user.acl[0]).toEqual(
186 | expect.objectContaining({ module: 'm1', field: 'f1', rights: {read: true}}),
187 | );
188 | });
我认为它失败是因为它user
是一个猫鼬对象。
解决方案
在比较之前,您应该_id
从对象中删除。
或者,您应该使用toMatchSnapshot()
(但仍需要删除每次运行时会更改的任何键。
推荐阅读
- c - math.h,无法正常工作,它会冻结 Tiva C 板 [嵌入式系统和 C 语言]
- video - 即使唯一可用的字幕是俄语和自动翻译,是否可以在 youtube 中下载自动生成的字幕的翻译版本?
- java - 使用类加载器从 Maven 依赖项加载类
- log4j - Kafka Connect 日志写在昨天的文件中
- deep-learning - Variational Autoencoder KL散度损失爆炸,模型返回nan
- ansible-template - 错误是一个
,原始消息:运行 ansible playbook 时没有名为 'equalto'"} 的测试 - jenkins - Jenkins 无法执行 NPM 命令
- python - 如何检测此 QR 码的位置,如对象并拆分为单元格?
- ruby-on-rails - 在 rails db:migrate 时,是否有可能在两次迁移之间发生一些写入?
- python - 使用 BeautifulSoup 使用下拉列表刮取表格内容