嗨,我想使用正则表达式检查传递给函数的参数。情况是我想验证一个对象,该对象包含在某些部分具有随机字符的生成的字符串
这是要检查的字符串
- 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)}));