BDD style testing in qunit

31 Aug 2015

Before the vacation I was working with Karl Adriansson on a project as the front end programmer. I helped get a front end JavaScript parts back on track. While doing this, Karl and one of the testers supplied me with specifications from the requirements. Since the specifications where very detailed, It was very easy to transform them into sort of BDD style tests:

test("PROJ-783", function (){
  var ctx = given_a_customer_previously_bought(black_sweater());
  and_I_currently_have_X_of_Y_in_stock(ctx, 3, black_sweater());
  when_he_returns_product_X_for_refund(ctx, black_sweater());
  then_I_should_have_X_of_Y_products_in_stock(ctx, 4, black_sweater());

Note that I’ve taken the specification from wikipedia in order to have a meaningful sample. We used the convention of writing the jira id as the test name in order to know from where the specification came.

Since we are using ordinary language constructs (methods that take parameters), the level of abstraction here is relatively low. Many of the helper methods can in many cases be reused since you might have many specifications related to the same subdomain. Some of the helper methods will only be used once. In some cases you might want to drop some of the prefixes in order to avoid to much duplication.



Do you want to send a comment or give me a hint about any issues with a blog post: Open up an issue on GitHub.

Do you want to fix an error or add a comment published on the blog? You can do a fork of this post and do a pull request on github.