Sinon 模拟期望:使用正则表达式检查函数参数对象字段字符串?

Sinon mock expectation: check function argument object field string using regex?

您好,我想使用正则表达式检查函数传递的参数。案例是我想验证一个包含生成的字符串的对象,该字符串在某些部分具有随机字符

这是要检查的字符串

- apple<randomchar>
- banana <randomchar>
- melon <randomchar>

以上文本已生成,可能会在 运行 之间更改。

我试过使用这个 sinon.match,但是找不到详细的文档,所以不确定这样做是否正确。

const sinon = require('sinon');

const foobar = {
    foo: () => {},
    fooWithObject: () => {},
};
const sinonMock = sinon.mock(foobar);

const textfoobar = (
`- apple randomid
- banana randomid
- melon randomid`
);

sinonMock
    .expects('foo')
    .withArgs(sinon.match(/apple.*banana.*melon/gms));
sinonMock
    .expects('fooWithObject')
    .withArgs({message: sinon.match(/apple.*banana.*melon/gms)});

// this works
foobar.foo(textfoobar);
// this doesn't
foobar.fooWithObject({message: textfoobar});

sinonMock.verify();

如果我将消息包装在一个对象中,则会出现上述结果错误。如何使用正则表达式检查包含字符串的参数调用对象?

原来,还需要在容器对象中使用sinon.mock。 所以这有效

sinonMock
    .expects('fooWithObject')
    .withArgs(sinon.match({message: sinon.match(/apple.*banana.*melon/gms)}));