meeshkan / unmock-js

Fuzz test your REST API calls
https://unmock.io
93 stars 8 forks source link

What is the right way to use responseBody().minItems(n)? #403

Closed shaabans closed 4 years ago

shaabans commented 4 years ago

I've been trying to understand how to control the number of items coming back in an array. Here's an example:

unmock
  .nock('https://.../api', 'myApi')
  .get('/cases')
  .reply(200, {
    status: 'ok',
    id: u.integer(),
    cases: u.array({
      id: u.integer(),
      created_at: Date.now(),
      updated_at: Date.now()
    })
  });

The following does not change how many cases items are returned:

myApi.state(
  withCodes(200),
  responseBody().minItems(20)
);

Maybe it shouldn't work since I'm not using u.array at the top level of the response, in which case can anyone provide a tip on how to specify which response element you want to set minItems on?

Much thanks in advance!

mikesol commented 4 years ago

Hi! Apologies it has taken us some time to look into this - one of the core maintainers will debug and should have a response by tomorrow!

shaabans commented 4 years ago

Thanks Mike, I appreciate how response you always are to questions. I'd be happy to submit a documentation update PR once I get my brain around how to use u.array.

mikesol commented 4 years ago

Hi! unmock does not currently have a global override for minItems of an array, so it needs to be specified on a property-by-property basis. In this case, you'd need to specify it for the cases property like so:

myApi.state(
  withCodes(200),
  responseBody({ lens: ["cases"] }).minItems(20)
);

In general, lens takes an array that drills down to the property. So, for example, if cases were nested in an object, ie {people: { cases: []}}, the lens would be { lens: [ "people", "cases"] }. I hope that's helpful, and if you have any suggestions for the API please let us know!

shaabans commented 4 years ago

This is perfect, exactly what I wanted to do!

I was going to suggest an update to https://www.unmock.io/docs/setting-state but it looks like there's already an example of using lens near the bottom which I must have missed.

Thanks again for the quick reply, knowing how to do this really helps us use Unmock.