我如何testing具有随机值的属性的对象?
我正在写一个unit testing,我正在嘲笑一个对象(客户端)有一个_request方法,期望一个对象和callback函数。 对象参数有一些随机值的属性:
var clientMock = sandbox.mock(client); // client is defined up somewhere clientMock .expects('_request') .withArgs({ method: 'POST', form: { commands: [{ type: "item_add", temp_id: '???', // <== This is random value uuid: '???', // <== Another random value args: { ... } }] } }, sinon.match.func);
我如何设置一个testing呢?
或者,我怎么能忽略这些特定的属性,并testing其他?
谢谢。
sinon.match
将帮助你
sandbox.mock(client) .expects('_request') .withArgs({ method: 'POST', form: { commands: [{ type: "item_add", temp_id: sinon.match.string, // As you probably passing String uuid: sinon.match.string, // As you probably passing String args: { ... } }] } }, sinon.match.func);
================
sandbox.mock(client) .expects('_request') .withArgs(sinon.match(function(obj) { var command = obj.form.commands[0]; return obj.method === 'POST' && command.type === 'item_add' && _.isString(command.temp_id) && _.isString(command.uuid); }, "Not the same!"), sinon.match.func);