gpt4 book ai didi

javascript - 用于检查对象数组中某些字段的 Chai 断言

转载 作者:行者123 更新时间:2023-12-05 07:37:41 33 4
gpt4 key购买 nike

我有一个如下所示的对象数组,并且只想针对数组中的某些字段进行断言。

对象数组 -

var a = [{"key":"key2","value":"value2","updatedBy":"name1","Dttm":1516849776062},{"key":"key1","value":"value1","updatedBy":"name2","Dttm":1516849776060}]]

我只需要对键和值字段进行断言,而不用担心 updatedBy 和 Dttm,所以类似的,

expect(a)........([{"key":"key1","value":"value1"},{"key":"key2","value":"value2"}])

最佳答案

您可以组合使用 2 个 chai 插件:chai-thingschai-like

Chai Things :

Chai Things adds support to Chai for assertions on array elements.

Chai Like :

A JSON matcher for chai. This is really useful when you are testing API and want to ignore some attributes like: updatedAt, createdAt, id.

例如,您可以使用两者编写这样的测试:

//...
expect(a).to.contain.something.like({"key":"key1", "value":"value1"})
expect(a).to.contain.something.like({"key":"key2", "value":"value2"})

安装方法如下:

npm install --save-dev chai-things chai-like

安装后告诉 chai 使用新插件:

const chai = require('chai')

chai.use(require('chai-like'))
chai.use(require('chai-things'))

进一步阅读和示例的链接:

Chai 东西:http://chaijs.com/plugins/chai-things/

Chai 样:http://chaijs.com/plugins/chai-like/

关于javascript - 用于检查对象数组中某些字段的 Chai 断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48435308/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com