我正在寻找匹配以下内容的最佳方式:
expect([
{
C1: 'xxx',
C0: 'this causes it not to match.'
}
]).to.deep.include.members([
{
C1: 'xxx'
}
]);
以上不起作用,因为C0存在于实际中,但不是预期的。简而言之,我希望这个期望通过,但我不知道如何在不写一堆自定义代码的情况下做到这一点......
答案 0 :(得分:8)
chai-subset或chai-fuzzy也可能会执行您正在寻找的内容。
Chai-subset应该像这样工作:
expect([
{
C1: 'xxx',
C0: 'this causes it not to match.'
}
]).to.containSubset([{C1: 'xxx'}]);
就个人而言,如果我不想包含其他插件,我将使用chai包含的property
或keys
匹配器:
([
{
C1: 'xxx',
C0: 'this causes it not to match.'
}
]).forEach(obj => {
expect(obj).to.have.key('C1'); // or...
expect(obj).to.have.property('C1', 'xxx');
});
答案 1 :(得分:4)
有一些不同的chai插件都可以解决这个问题。我是shallow-deep-equal的粉丝。你可以这样使用它:
expect([
{
C1: 'xxx',
C0: 'this causes it not to match.'
}
]).to.shallowDeepEqual([
{
C1: 'xxx'
}
]);
答案 2 :(得分:2)
没有插件: http://chaijs.com/api/bdd/#method_property
expect([
{
C1: 'xxx',
C0: 'this causes it not to match.'
}
]).to.have.deep.property('[0].C1', 'xxx');
答案 3 :(得分:0)
我认为最简单(当然最简单)的方式是:
var actual=[
{
C1:'xxx',
C0:'yyy'
}
];
actual.forEach(function(obj){
expect(obj).to.have.property('C1','xxx');
});
答案 4 :(得分:0)
我写了chai-match-pattern和lodash-match-pattern来处理部分匹配(以及更多)深度匹配方案。
var chai = require('chai');
var chaiMatchPattern = require('chai-match-pattern');
chai.use(chaiMatchPattern);
// Using JDON pattern in expectation
chai.expect([
{
C1: 'xxx',
C0: 'this causes it not to match.'
}
]).to.matchPattern([
{
C1: 'xxx',
'...': ''
}
]);
// Using the slightly cleaner string pattern notation in expectation
chai.expect([
{
C1: 'xxx',
C0: 'this causes it not to match.'
}
]).to.matchPattern(`
[
{
C1: 'xxx',
...
}
]
`
);
答案 5 :(得分:0)
#RobRaisch的版本稍有更新,因为对于空比较,由于“不等于””而产生错误
let expected = {
dateOfBirth: '',
admissionDate: '',
dischargeDate: '',
incidentLocation: null
};
Object.keys(expected).forEach(function(key) {
expect(actual[key]).to.equal(expected[key]);
});
答案 6 :(得分:0)
您可以使用pick
和omit
下划线功能选择/拒绝要测试的属性:
const { pick, omit } = require('underscore');
const obj = {
C1: 'xxx',
C0: 'this causes it not to match.',
};
it('tests sparse object with pick', () => {
expect(pick(obj, 'C1')).to.eql({ C1: 'xxx' });
});
it('tests sparse object with omit', () => {
expect(omit(obj, 'C0')).to.eql({ C1: 'xxx' });
});
答案 7 :(得分:0)
只需使用地图即可过滤您要检查的密钥
类似:
Sub Macro1()
Dim lastRow As Long, i As Long
lastRow = Cells(Rows.Count, "K").End(xlUp).Row
For i = 2 To lastRow
If Cells(i, "J").Value = "WCblack" Then
Range("AY2:AY" & lastRow).Formula = "=TRIM(LEFT(AW2,FIND(CHAR(10),AW2)-1))"
If Cells(i, "AY").Value = "Color: Red" Then
Cells(i, "J").Value = "WCred"
ElseIf Cells(i, "AY").Value = "Color: Gold" Then
Cells(i, "J").Value = "WCgold"
ElseIf Cells(i, "AY").Value = "Color: Blue" Then
Cells(i, "J").Value = "WCblue"
End If
End If
Next
End Sub
答案 8 :(得分:0)
如果您需要将其与 spy 和 Called.with 一起使用,请检查此答案:https://stackoverflow.com/a/58940221/1151741
例如
expect(spy1).to.have.been.called.with.objectContaining({ a: 1 });