A small number of projects here in New York have adopted my extremely simple behaviour driven development library, SimpleBDD, and I thought I’d share some of the emerging patterns those teams have developed while using it.
SimpleBDD, is a way of using a Gherkin like specification language and turning into a method call with Ruby. It takes the approach of tools like Cucumber but reduces it down the smallest set of features. Essentially taking a method call:
Given "a logged in user"
and calling an underlaying function:
a_logged_in_user
If that method is in the scope of the executing test, that method is executed, if the method isn’t in scope or doesn’t exist the standard method not found exception is raised. It enables a developer to produce Gherkin like specifications while staying in Ruby, using the test framework of choice.
I generally try and follow the advise of my colleague, Matt Parker, with his excellent post on steps as teleportation devises. We try and create a reusable and stateless domain specific language (DSL) for our tests and our steps call into the DSL and hold state pertinent to that particular test run.
At first on my current project we have had three separate areas for our request specs. We have the request spec itself which used SimpleBDD to describe the behavior of the application. We then had a ‘steps’ file which had the methods calls from the SimpleBDD and translated those into the reusable DSL for the application. The steps file was reused across all request specs and was becoming big pretty quickly.
Dirk, on another project which is also using SimpleBDD, skipped the ‘steps’ file and placed those methods straight into the rspec feature files underneath the scenario blocks. Then after some discussions with JT on where to keep the state our tests depended on, Brent and our team started using rspecs ‘let’ methods and the ‘steps’ within the scope of the feature block to keep the intention of the test in one place.
By also putting more responsibility onto the DSL, these methods are pretty dumb, leaving the test describing what the test is attempting to achieve through SimpleBDD method calls and the how through calling the DSL via the ‘step’ methods within the feature block in the same file.
require 'spec_helper'
feature 'homepage' do
scenario 'happy path' do
Given "an existing user with one widget"
When "the user visits the homepage"
Then "the user can see their widget"
end
let(:user) { FactoryGirl.create(:user) }
let(:widget) { FactoryGirl.create(:widget) }
def an_existing_user_with_one_widget
user.widgets << widget # Using the application code to create initial state
login user # Application DSL
end
def the_user_visits_the_homepage
visit root_path # Capybara DSL
end
def the_user_can_see_their_widget
can_see_widget widget # Application DSL
end
end
This has been working out fairly well for us and if you’re interested in a simple version for BDD for your project I hope you check this out.