Working with BDD
The concept of Behaviour Driven Development (BDD) is pretty simple. You describe what you want the system to do by describing potential users interactions with the different parts of the application. You work outside-in to implement features using the examples to validate that you're building the right thing at the right time. During this workshop you will see BDD in action and will experience, at least partially, the benefits of this method.
The tools we will be using to test our application are RSpec and Cucumber. Those are the two so called testing frameworks that will help us to write good code. During the development process, we'll take an approach that combines high level acceptance tests and low level unit tests to both drive the development process and make sure that we build a robust and well structured application.
Test first
Let's start with writing some high level acceptance tests.
What we want to do at this stage is to get the user stories we have defined and look at them from a user's perspective. Also, what an actual implementation of features would look like. In short, we want to take each user story and break it down to scenarios that each represents a use case in the application. Sounds confusing? It is, but look at it as a form of a blue print that you will use when we start to actually build the app.
Cucumber (cucumber.io) is a testing framework used to describe high level functionality of your application. We will refer to them as acceptance test or features. One of Cucumber's most compelling features is that, it allows you to write these descriptions in plain text. Even in your native language.
Cucumber is not installed on your system so we need to do that as a first step.
Open your terminal and let's setup the project folder:
Now run the following command..
..and watch the system install it together with some other libraries it depends on in order to be able to run,
Once that is complete I'd like you to initiate Cucumber by simply typing in:
That will create some files and folders for you:
Okay, now, in your terminal, you type in:
The output you will see should look something like this:
Alright, we have installed and initiated the first testing framework. Big step.
Now let's take a moment to talk about how a Cucumber file is built and then we'll write our first of many tests.
A feature is defined by one or more scenarios. A scenario is a sequence of steps through the feature that exercises one path.
A scenario is made up of 3 sections related to the 3 types of steps:
Given:
This sets up preconditions, or context, for the scenario.When:
This is what the feature is talking about, the action, the behavior that we're focused on.Then:
This checks postconditions. It verifies that the right thing happen in theWhen
stage.
There is yet another type of step you can use in a scenario path, and that is the And
keyword. And can be used in any of the three sections. It serves as a nice shorthand for repeating the Given
, When
, or Then
. And
stands in for whatever the most recent explicitly named step was.
In the newly created features
folder, please create a course_create.feature
by returning to your terminal window and typing in:
Open that file and add the following code:
The Feature:
description is added mainly for your and your fellow project members reference. It tells a reader what this test file is actually about - setting the scope for the scenarios.
As the next step, we will add two basic scenarios:
Let's have a look at the first one:
The 2 preconditions for our scenario is that we have logged in to the application and are currently visiting the root path of the app.
The action I'm taking is clicking on the
All courses
link.The postcondition is the fact that I see the message "You have not created any courses"
Now in your terminal window, go ahead and type cucumber
again and watch the test framework do its job. If you have followed my instructions, you will see an output similar to this:
And also:
This is perfectly normal, and means that we need to start doing some serious programming in order to actually implement the tests and eventually make them pass.
Last updated