Thursday, 27 April 2017

BDD + TDD +UADD - Part I

It is rare that different frameworks can be wired together without facing some glitch in the implementation path. Sometimes, the technologies pose problems, some other times, the tools are not available.

For instance, on a previous occasion, I tried the three with the combination of Slim, C#, NUnit and it developed hiccups because, being a web application, the Javascript/jQuery code could not be as fluently unit tested even though I tried Jasmine and Slim posed yet another problem in that it required separate DLLs to be created for the functional tests to happen, which was far too cumbersome because it meant that components were getting created just so that some tests [to make a framework work] could be performed.

Not what a good design is - you do not create components to make tests succeed, you create components to make the system function properly!

The key to creating a great design, the Agile way, is to let the tests drive the design, not just the development. This can be possible only by getting the requirements right, right upfront. To achieve this, BDD (Behavior Driven Development) framework comes handy.

In this context, naming this framework as a Testing Behavior [Business Driven Behavior] for better User Acceptance (TBUA) would be the closest acronym to describe it.


There are many tools and definitions that go around for BDD.

The most famous ones are

1. promise,
2. given, when, then...,
3. should and expect

style of unit testing and,

what I consider to be true behavior driven development -

4. scenario testing based unit tests.

The flavors are different with the intent same - that of providing for as many tests as possible for any given requirement [line of code].

For instance, a Business requirement is to open a XML file, read the contents and get a string as result.

The test scenarios for this business requirement are as follows:

1. Check if file exists
2. Check if file is an XML [filename contains .xml extension]
3. Check if valid XML
4. Check if string generated from XML

The difference between the BDD [without scenario testing] and the BDD [with scenario testing] is highlighted in the above screenshot.

The scenario as described by the Business Rule is to check if the file exists.

But since there is only the business requirement available at this stage, and no coding done yet, the method "openFile" does not exist. What this means is that BDD helps track the business requirements correctly. With scenario tests, it becomes easier to keep the development strictly within the business context.

The scenario has been documented in fitnesse's slimjs tool. Expand the scenario name

This part of the BDD is so essential because it helps trace the entire lifecycle of development from the time the Business Analyst comes up with the business rules based on the requirements and elaborates the scenarios for user acceptance to the time when UACs are contested over.

The rest of the scenarios are

The scenario, check if file extension is xml, does not call a method because it is well possible that it would be handled by the openFile method.

This choice is what makes the Agile way of development so different because deferring the decision till later when coding actually will reveal the possibilities through design evolved from unit tests, allows for a higher code quality.

At this point of the lifecycle, the BDD has just started. Some scenarios have been obtained from user stories and scenario tests have been written for it.

The main reason for writing scenario tests is so as to be able to wire up the scenarios with functional tests, after the unit tests [part of TDD] have completed.

Please note it is not necessary to do scenario testing to do BDD; it just enhances the Agile experience more.

Next part: Deriving unit tests from scenario tests

Monday, 17 April 2017

SinonJS - the spy

The example is more of a revealer to an error that can be quite vexing in sinon, node js parlance.

It is called "attempted to wrap undefined property as function" as if it were an attempt to murder.

The case in question is that of a call made to sinon's spy method

var theSpy = sinon.spy('get');
If used as such, this line will throw the error. The trick is to use the container that contains the 'get' method as the first parameter:

Not much of a sinon, but a spy nevertheless, much like Johnny English!

The spy method of sinon helps you to understand how many times a method call happened and it can also help you understand that if at all a call did get made to a method. This can be helpful in calls over the wire, say, http.

You need mocha, sinonjs and nodejs to run the example.

Below is the code.

var assert = require('assert');
var sinon = require('sinon');

var http = require('http');
var api = require('../myapi.js');
describe('testingBlogPost', function() {
var theSpy = sinon.spy(api,'get'); //  the first parameter will resolve the undefined property method

it('should get result to compare', function() {
var expected = 'Moved Permanently';
api.get(function(err, result) {
api.get(function(err, result) {


var http = require('http');
function myApi(){};

myApi.prototype = {
get: function(req,res) {
var req = http.request({
hostname: '',
path: '/2017/04/how-to-setup-middle-game.html'
}, function(response) {
var data = '';
response.on('data', function(chunk) {
data += chunk;
response.on('end', function() {
module.exports = new myApi();

And the result, because the get method was twice called

Of course, another reason for writing the test also helps in knowing what if bots were to call posts on my blog! :)

Happy spying!

Tuesday, 4 April 2017

How to setup the middle game

It was way back in 1985. I was the captain of my college chess team and we were playing the Delhi University's Inter-College Chess Championships.

Our college, Sri Venkateswara College, had been a finalist in the previous year (first time in the history, I recall somebody saying it during the sports meetup) and since the captain had attributed that it was largely due to my efforts, I was offered the captaincy by the college principal and the Sports chief! I can just about recall that I barely managed to accept the captaincy because I was hugely reticent then.

We were playing the winners - Kirori Mal College - and they were formidable.

I had played the different boards in the previous championships so I knew how the order was set and the strategies played.

I knew that there were the smaller fishes that I could always attack, later, as the tournament format, being the swiss rounds, progressed and eyed the larger ones for just a point or a two. So, I decided to play the weakest on the top board, against a later-to-become-Delhi State champion (or possibly the then Delhi state champion), and myself played the 3rd board, hoping to at least fetch a half point for the team.

I don't recall the outcome much but I do recall that I decided to play the 2nd board in the next round against - another formidable team - the St. Stephen's ...! It had another well-known player on the top board and the second board was played by a fair girl, who, too, was a No.2 in the Women's category of the State level!

I decided to chance my luck against her and played the 2nd board!

To put it in perspective, I remembered this game because this post is about how a good opening can lead to a good middle game and a solid end game.

She played the French defense.

I say that with reverence.

Ever since I had eyed her on the floor, tall, fair, slender and very good looking, I had developed a crush on her. And there I was, playing against a ...e6!

In all my preparations, that mostly included games of the Rusian Grandmasters and a Chess Mate subscription that I had won in an Open tournament, the French Defense was the one that I had avoided, not because I did not like it but because Fischer demonstrated some stuff that put me off it forever!

I should have known. A slender soft female would go for the French Defense. It is docile, meek and extremely defensive. Of course, I loathe the opening and not just because I lost the game even before it reached the middle game for me to get my combinations going, but because, at that age, it seemed like I had lost some right to woo her! I still remember that feeling - a squashed out Bertie-Wooster-type-after-meeting-Madeline kind.

Since then I rarely play e4 in games of importance.

So I decided to check out d4 when I started playing Chess again around the early 2000s, with opponents who were four former Delhi State Champions, at an outdoor park, where we gathered each day over many months to play Chess, and which led to my book on Chess!

The only weapon that I had or could muster, against their class and experience, was to play some unconventional Chess and I started to explore Bf4 after Nf3 and not C4, as is the normal.

One of the former champions, the co-author of the book, "Analytical Games of Chess", remarked on why I didn't play the usual Queen's gambit C4, and the reason behind the move. I said that I am very comfortable playing the move. That was not the whole reason.

The real reason is that Bf4 provides the means to attack the black queenside as well as be in a position to switch to a kingside attack, without being obvious.

Another key element, in this strategy, is to sustain the diagonal's importance and make Black pay for moves like Be7 (instead of Bd6) and so, it is equally important to play h3 as soon as Black plays Nf6.

And yet another advantage is that it guides Black to play Ne4 as Nh5 could make no difference as the Bishop could retreat to h2, without causing any double pawns at f4 or g3.

Like so.

Take no notice of the Computer saying it is +0.80. It is just bunkuming itself! A human would not have played g5 before castling, anyway.

The middle game becomes a cake walk.

As I said, take no notice of what the computer thinks of a position.

It says "Checkmate" at the end, too, when it got checkmated - as if it was announcing it to me! :)

Below is another very good game to demonstrate the power of Bf4, in which the white Bishop in h2 makes life miserable for the entire diagonal, all the way to b8 and, to protect the c7 pawn or free the pressure, black has to think of a different strategy to play the Knight via d7!

Check out the difference in the two games - one, played by a computer and the other, by a human - played with the same strategies but the attack varies, depending upon the moves by Black.

The key to playing Bf4 is to have a strategy that mobilizes the a, b and c pawns to build the pressure on black's queenside. More often than not, it works.

It is important to note that with Bf4 and no c4, the White Bishop, in the opening, must be played, always, to Be2 and not to Bd3 or to Bc4 if attacking black's queenside is to succeed. Always.