roboli
roboli

Reputation: 1478

How can I test an object with properties with random values?

I'm writting a unit test and I'm mocking an an object (client) which has a _request method which expects an object and a callback function. The object param has a couple of properties with random values:

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);

How can I set a test for that?

Or how can I ignore those specific properties and test the others?

Thanks.

Upvotes: 3

Views: 996

Answers (1)

Krzysztof Safjanowski
Krzysztof Safjanowski

Reputation: 7438

sinon.match will help you

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);

Upvotes: 2

Related Questions