million-views / praas

Proxy as a service.
MIT License
4 stars 2 forks source link

Refactor backend tests... #158

Closed m5nv closed 4 years ago

m5nv commented 4 years ago

Context The most expedient way to test anything is to be exhaustive. This is called the powerset model. Easy to get started but can cost us in the long term, coz code is liability. The correct model to hold when writing a test is to ask:

  1. what it does for code coverage, more is better
  2. how it should identify incorrect behavior of the subject under test without using a powerset model

Problem There are just too many test cases that do not add value. Value here would be to increase code coverage with the least number of test cases.

For example:

sub-context-of-conduit-testing: 'testing suri field...'
  - 'should not allow no suri'
  - 'should not allow undefined suri'
  - 'should not allow null suri'
  - 'should not allow empty suri'
  - 'should not allow non-url suri'

Can be reduced to:

// unhappy path, happy path is covered elsewhere... 
// notice there is no sub-context just to test a field
- 'should reject invalid suri'
  - test for null, undefined (note: in JS empty, undefined, null are falsey)
  - test for invalid url

Exemplar

it('should reject invalid suri', async () => {
  const ct = await helpers.fakeConduit();

  // in js: blank, undefined, null, not-present are all falsey
  ct.suri = undefined;
  const res = await Api()
    .post(`/conduits`)
    .set('Authorization', `Token ${jakeUser.token}`)
    .send({ conduit: ct });
  expect(res.status).to.equal(422);
  expect(Object.keys(res.body.errors)).to.include('suri');

  // test for invalid url, notice we are re-using `ct`
  ct.suri = 'not-in-url-format';
  const res = await Api()
    .post(`/conduits`)
    .set('Authorization', `Token ${jakeUser.token}`)
    .send({ conduit: ct });
  expect(res.status).to.equal(422);
  expect(res.body.error.errors[0].path).to.equal('suri');
});

Acceptance Criteria

shinenelson commented 4 years ago

Sorry, that was an accidental click