I started on another side project, which I've dubbed Bunch of Stuff, and I've continued to appreciate the practicality of Behaviour Driven Development, especially with Cucumber on Ruby on Rails. I found that I'm more effective and efficient with prototyping when using these practices than when I'm not.
For some context, I no longer get to code in my day to day job, and I find myself missing the joy of code tinkering. Every so often, I have a new idea or problem in my life that I think an application can solve. However, I only get to it a couple of times a month. Because of that, I never consider the code I create intended for full production, but they often bounce between proof of concept and prototype. This post is to share some of the benefits that I really appreciate from the practice of leveraging Behaviour Driven Development in prototyping.
The human readability through Cucumber is the feature I appreciate most about it. I appreciate that it doesn't have the distraction of code embedded within it and makes it usable by anyone. It also makes the test portable to other code bases as it is independent of the implementation.
Keeping Track Where I Left Off
When you're jumping into code only every week or so, it's often hard to track where you left off without having to run through your code in the hopes that you'll see where the code breaks. I've found Cucumber to be a really powerful tool to combat this. Before I start coding, I write my code using the Gherkin format, which looks like this:
Scenario: Administrators can create new categories Given I have an "administrator" user in the system And I am signed in to the system And I have 1 category in mind And I am on the "Administration" page When I click on the "Create new category button" And I enter the category 1 details And I click on the "Submit new category button" Then I will be on the category 1 detail page @wip Scenario: Administrators can see categories in admin page Given I have an "administrator" user in the system And I am signed in to the system And I have 5 categories in the system And I am on the "Administration" page Then I will see 5 "categories" on the page
I really appreciate that the story is written in a way that is human-readable and easy to track what I'm trying to accomplish through code. The other mechanism I use is the tagging that comes with Cucumber. When I come back to my code, I run the cucumber command with the work in progress tag to isolate the scenario I'm coding for, and it is broken where I left off.
I like how Cucumber separates the requirements through their feature files from the tests through their step definitions. I've started to build a library of generic step definitions that can handle a bulk of my different steps. This means that I can focus on creating my scenarios without over-investing in writing out code for testing.
While it's not the main driver, it's definitely a benefit. The Cucumber gem in Ruby integrates nicely with SimpleCov and accounts for my code coverage. It's a nice boon from a code health metric.
Given that my code usually teeters between Proof of Concept and Prototype, I don't do the full battery of tests. Very quickly, you can feel where unit tests would be so helpful in helping you identify where things are broken. The purpose of using BDD in my scenario is that it would be good enough testing for prototyping, and I would never push the code to production without an integral unit and other integration tests.
Behaviour Driven Development with Cucumber in Rails is a convenient way to get good enough testing for prototyping applications. It allows me to be able to track where I am in my prototyping, not to overinvest and gives me just enough testing so that I can comfortably create my prototype and know when I've broken something along the way.