Last Updated: February 25, 2016
·
562
· davidrhyswhite

Add spec style tests in Dart with 2 lines

If like me you prefere spec / behaviour driven testing syntax over traditional unit style then moving to a new language like Dart with no BDD libraries currently can be a bit of an annoyance.

Just aliasing group and test can give your testing file a whole new life.

void describe(String description, void body()) => group(description, body);
void it(String spec, TestFunction body) => test(spec, body);

You can now format your tests like this:

describe('Point', (){
  var point;
  it('should set x on init', () {
    var expectedX = 10;
    point = new Point(expectedX, 20);
    expect(point.x, expectedX);
  });

  it('should set y on init', () {
    var expectedY = 20;
    point = new Point(10, expectedY);
    expect(point.y, expectedY);
  });
});

For me describe and it calls are far more readable than group and test calls, but it's a personal preference.