Book Image

Learning Behavior-driven development with Javascript

Book Image

Learning Behavior-driven development with Javascript

Overview of this book

Table of Contents (17 chapters)
Learning Behavior-driven Development with JavaScript
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Red/Green/Refactor


Now that we have a basic knowledge of Chai, we can continue with our coding. The code we already have in place is clearly not correct, so we need to add another test that forces us to make the code right.

Currently, we are implementing the rule about errors for nonpositive numbers, so we should finish it before continuing. Which test could we add to make our implementation fail? We can try testing the validator with valid numbers. Let's see the result:

describe('A Validator', function() {
  it('will return no errors for valid numbers', function() {
    expect(validator(3)).to.be.empty;
  });

  it('will return error.nonpositive for not strictly positive numbers', function() {
    expect(validator(0)).to.be.deep.equal(['error.nonpositive']);
  });
});

Note that we have added a new test for valid numbers that is failing. So now we fix it in a very simple way in lib/validator.js:

module.exports = function (n) {
  if(n === 0)
    return ['error.nonpositive'];
  return [];
};

The...