mercredi 15 juillet 2015

mocha nesting/replicating tests

Given a data structure that satisfies some invariants, I would like to test the state of an instance of the data structure after various operations. What is the best way to do this?

describe('data-structure', function() {
  var x;

  beforeEach(function() {
    x = getDataStructure();
  });

  describe('satisfies invariants', function() {
    // run tests on 'fresh' x

    it('should ...', function() { 
      // ...
    });

    // ...
  });

  describe('operation 1', function() {
    it('should preserve invariants', function() {
      x.doSomething();
      // run 'satisfies invariants' tests on modified x
    });
  });
});

I thought about using an afterEach hook, but I do not think x is preserved there?

afterEach(function() {
  // somehow run 'satisfies invariants' test
});

It maybe be that I can refactor 'satisfies invariants' into a method, but it would be nice if mocha could report which invariant-tests failed for each operation, e.g.

data-structure
  satisfies invariants
    should satisfy invariant 1 ...
    ...

  operation 1
    should satisfy invariant 1 ...
    ...

  operation 2
    should satisfy invariant 1 ...
    ...

Aucun commentaire:

Enregistrer un commentaire