Main Content

Behaviour and test driven development in Ruby using RSpec

Archive - Originally posted on "The Horse's Mouth" - 2015-11-21 12:48:43 - Graham Ellis

RSpec is a Test / Behaviourish Driven Development Gem for Ruby that you'll commonly find used in associaition with Rails, Gherkin, Cucumber, Watir, Selenium, Capybara ... but it can also be used stand alone. One of the big problems of starting to use it stand alone is to find an example which doesn't bring in lots of those other bits of softare and lots of complexity. Sample test source [here] ... described in a previous blog [here].

That's "Hello Rspec World" ... but I went on during the course just completed to add a loop with multiple sets of valued in the tests [here] and then to load the class being tested from a file, set up a table of test data and the results expected as I defined my behaviour, and ran multiple tests on each line of the data. Test program [here] and class being tested [here].

Note the use of describe, before and it blocks.

describe defines a whole group of tests

it defines an individual test

before describes the pre-test (it) setup that's to be done for each and every test in the group.

Although RSpec is an important tool to some Ruby users - indeed it can be a deciding factor in choosing to use Ruby - the use of the Gem is straightforward and I'm happy to cover it during a private Ruby course, or to spend an extra hour with delegates first thing in the morning on the final day of a public Ruby course.