I'm writing a utility that validates fields. I decided to try my hand at Behaviour Driven Development (BDD). The validator utilises "rules" to determine if field is valid.
Three different types of rules exist:
RegexRule
A field conforms to specific pattern(s)RequireRule
A value is presentEqualityRule
A field's value equals another's value
A rule is tested against a value. If the value does not conform to the rule, a flag is set which marks the rule as erroneous. Calling getError()
on an erroneous rule returns a string that details the error. Furthermore, the rule is reset (no longer erroneous).
Is this the correct manner of implementing BDD on this class? I'd greatly appreciate advice on how I could improve on this.
describe('RequireRule', function() {
var nameErr = 'The name field is required';
beforeEach(function() {
this.r = new RequireRule('name', nameErr);
});
describe('test()', function() {
it('should return false when value provided is null or undefined', function() {
expect(this.r.test('asd')).to.equal(true);
});
it('should return true when value provided is not null or undefined', function() {
expect(this.r.test(null)).to.equal(false);
});
});
describe('isErroneous()', function() {
it('should become erroneous when value tested is null or undefined', function() {
r.test(null);
expect(this.r.isErroneous()).to.equal(true);
});
it('should not be erroneous when value tested is not null or undefined', function() {
r.test('asd');
expect(this.r.isErroneous()).to.equal(false);
});
});
describe('getError()', function() {
it('should return null when the test() succeeded', function() {
r.test('asd');
expect(this.r.getError()).to.equal(null);
});
it('should return the error string when the test() failed, and set erroneous to false', function() {
r.test(null);
expect(this.r.getError()).to.equal(nameErr);
expect(this.r.isErroneous()).to.equal(false);
});
});
});
The test framework is Mocha.
test()
expectations are reversed from their descriptions. \$\endgroup\$