I'm writing a utility that validates fields on a web site. I decided I'd give Behaviour Driven Development (BDD) a try. The validator utilises rules to determine if a value the user provided is valid.
Three different type of rules are used to validate fields:
RegexRule(field conforms to specific pattern(s))RequireRule(value is present)EqualityRule(field value equals another field's value).
A rule is tested against a value, and if the value does not conform to the nature of the rule, a flag is set, marking the rule as erroneous. Upon calling getError() on an erroneous rule, a string is returned detailing the error, and the rule is reset (no longer erroneous).
Is this the correct manner of implementing BDD on this class? If not, 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);
});
});
});