BDD with 3 languages - PHP with PHPSpec, Golang with Ginkgo and Javascript with Mocha

Sharpening Your SpecBDD skills

In these series I want to show differences (or similarities) between PHP, Google's Golang, and Javascript when you want to develop your code with BDD techniques. Why? One of reason is that You can easily port your specs to other language, spec is your project and your documentation and if you want learn some new language and You have already written what should be done then half of your work is done already. I was really suprised how easily I ported specs from Go to javascript.

TLDR; goto blog entries for first excercise directly

WiredToTheMoon Blog

Idea to do some simple BDD excercises came from http://www.wiredtothemoon.com/2012/12/12-tdds-of-christmas/. First I 've done that in PHP, after that I want to try the same in Golang which I was playing with, and because I'm doing some javascript research recently so I've wrote first excercises in Javascript too.

I have my solutions in 3 separate repositories one per language (+ bdd framework)

If You start your adventure with SpecBDD It'll be great boost for You to do whis wiredtothemoon excercises. If You have some ideas how to improve some of this excercises I'll be happy to hear about it.

Completed excercises

My Excercise status for languages of 12 tdds from wiredtothemoon blog

Here will be updates about my completed excercises.

No Excercise PHP Golang Javascript
1 Calculations spec src blog spec src blog spec src blog
2 Numbers spelling spec src spec src spec src
3 Bomberman game spec src spec src spec src
4 Monty Hall game spec src spec src spec src
5 FizzBuzz spec src   spec src
6 Recently used list spec src   spec src
7 Template engine spec src   spec src
8 Range class spec1 src1 spec2 src2    
9        
10        
11        
12        
         

I'll try to complete some more in near future

Excercise 2 - Number speller

Second excercise is little harder. Your task is to write number speller. You give a number and your function will spell it. Simple isn't it?

Excercise 1 - Numbers statistics

First excercise is really warm-up, it's really simple task to calculate some data (sum, avg, min, max, count) from given list of integers. You'll find My solutions below. (all exercises are commited to repository from given language and test framework)

Tools used for writing BDD specs

PHP - PHPSpec

It's modern technique of writing your tests/specs in PHP world from Konstantin Kudryashov (@everzet). It should be treaten like DSL (don't try to complain that it's not PSR compatible). You can find coverage tool for it too (but probably when You need it in spec way you are doing something wrong). I You are PHPUnit guy (girl) then You should give PHPSpec a try (If You don't write tests/specs for your new code hmm … start doing it now!).

Golang - Ginkgo

Syntax in Ginkgo is very familiar to many known tools (Rspec from ruby or Mocha from javascript) Your'e defining your expectations. It has many tools which make your Golang concurency easier.

Javascript (Nodejs) - Mocha

Mocha is written by TJ Holowaychuk author of very popular Express framework. It's really good piece of software. You can install assert library of your choice (I've chosed should.js). Mocha has nice async handling and could be used to write any kind of tests from spec/unit to end-to-end.

Setup

PHPSpec

Look at: https://github.com/exu/php-12bdds/blob/master/composer.json

If You're bad PHP boy without knowledge about composer you should take a pen and piece of paper and write: "I will use proper tools for my PHP development workflow!" two hundred times!

Remember to call

composer install

in your shell

Ginkgo

So You want to write some Golang code. Setup is really simple (https://github.com/onsi/ginkgo) Look at my repository for details https://github.com/exu/bddgo

Mocha

run:

npm install mocha --save-dev

and start writing your specs in tests directory.

Conclusions

I think that the slighty faster development time You can achieve in PHPSpec. Main reason for it are Its generators. Slighty more readable output in case of errors are in Mocha IMHO in PHPSpec diffs are sometimes annoying (you need to go into verbose mode) and when difference is in long string at the end you'll not receive valid diff.

comments powered by Disqus