Test Driven Development using Practical Meteor

Posted on Aug 09, 2017

Test Driven Development using Practical Meteor

When working in a project testing is the essential part as it will ensure what we have built is working as intended. Testing is the process which comes after the development, what if there is a way we can first test then we start developing, sounds not so good right?. Trust me I have the same feeling when I first heard about it. Well in my experience I found it very effective it’s like when you know what to do and what are you doing then you can never go wrong, that’s what ensured by Test Driven Development. So let’s understand what it is exactly and how it works.

 

What is it Test Driven Development?

Test Driven Development or TDD is a development process where you write test cases before development which will likely fail when you run these test cases as there is no functional code added yet. Next phase is to add the minimal functional code and rerun the test cases and refactor your code, repeat the process until all test cases pass.

TDD Cycle
    
So what’s the benefit

Well, now the programmer can be more confident that the new code meets the test requirement and doesn’t break or degrade any existing feature.There are a bunch of testing packages available for each environment but today I will show you how to perform TDD on node based Meteor framework using a package called practicalmeteor/meteor-chai so let’s get our hands dirty.

Basic Testing Example: 

I am creating test cases for a basic function which will be performing addition on two numbers, It will return when the arguments count will not be equal to two and the passed arguments are not a number.

The first test case will be in which it is going to return the expected value.

And we have to think of scenarios in which it is going to fail.

Scenario 1: Passed correct values and the correct number of arguments in the function:
    it('Got proper result ', () => {

      let result = addition(10, 20);

      expect(result).to.be.a('number');

      expect(result).to.equal(30);

    });
Scenario 2: Passed correct values but extra arguments in the function:
    it('Extra arguments', () => {

      let result = addition(1, 2, 3);

      expect(result).to.be.NaN;

      expect(result).to.equal('Please enter two arguments.');

    });
Scenario 3: Passed correct values but fewer arguments in the function:
    it('Less number of arguments', () => {

      let result = addition(10);

      expect(result).to.be.NaN;

      expect(result).to.equal('Please enter two arguments.');

    });
Scenario 4: Passed incorrect values with an incorrect number of arguments in the function:
    it('Invalid arguments', () => {

      let result = addition('xyz', 'abc');

      expect(result).to.be.NaN;

      expect(result).to.equal('Entered value is not a number.');

    });

Below is the screenshot when the test cases will pass.

Test case execution screenshot test driven development

P.S. – It’s time-consuming but once you start with this, you will save time in future as you got to know about all the cases before writing the actual code, i.e. where the code should fail and pass.