Blog

Introducing StepSpecr

By matthias

StepSpecr is a Rails plugin intended to be used with Rspec User Stories.

StepSpecr provides a ‘testing’ framework for speccing Given/When/Then steps within Rspec examples. This lets you implement GWT-steps the BDD way.

Getting started

StepSpecr is a Rails plugin, so…

$ rails greatapp
$ cd greatapp
It’s necessary to have edge rails in order to use the plugin script with github (where stepspecr is hosted).
$ rake rails:freeze:edge
StepSpecr depends on rspec:
$ script/plugin install git://github.com/dchelimsky/rspec.git
$ script/plugin install git://github.com/dchelimsky/rspec-rails.git
$ script/generate rspec
Install StepSpecr:
$ script/plugin install git://github.com/mhennemeyer/stepspecr.git
$ script/generate stepspecr

Run the example_step_spec

By bootstraping the project (script/generate stepspecr) a example_step_spec.rb was generated. Run it now to see if everything works.

$ script/spec—format -c specdoc spec/steps/example_step_spec.rb

( I'll work on performance :) )

Spec a great step

Create a new spec file:

$ mate greatapp/spec/steps/great_step_spec.rb
The spec needs to require its helper file:
require File.expand_path(File.dirname(FILE) + ”/stepspecr_helper.rb”)
(You can copy it from the example_step_spec.)

The greatapp must have a great model to work properly

so you’ll likely write the following step in your Rspec Story:
Given a great_model

Write an example for this step:

In PROJECT_HOME/spec/steps/great_steps_spec.rb:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

  require File.expand_path(File.dirname(__FILE__) + "/stepspecr_helper.rb")

  describe "Given a great_model" do
    it "should create a great_model" do
      StepSpecr.spec "Given a great_model" do 
        step_group :great_models
        before do 
          class GreatModel 
          end
          GreatModel.should_receive(:create)
        end
      end
    end
  end
  
The code passed to the before method in the spec block will be evaluated before the ‘Given a great_model’ step gets run.
We only want to specify what the step will do and we don’t care about if there is a GreatModel class in the system. – so we can just ‘mock’ one.
The second statement in the before block is the actual expectation we have of the behavior of the step: It should try to create a GreatModel instance.
Run the example and watch it fail:
$ script/spec—format -c specdoc spec/steps/great_steps_spec.rb

Implementation.

Create the step file:

$ mate greatapp/stories/steps/great_steps.rb
The failure message was: ‘Didn’t find step’. Let’s write the step without implementation to get a more meaningful directive:
1
2
3
4
5
6

  steps_for :great_models do
    Given "a great_model" do
    end
  end
  
Run the example again:
$ script/spec—format specdoc spec/steps/great_step_spec.rb
Now the system tells us what to do: ‘Mock expected :create … ‘
1
2
3
4
5
6
7

  steps_for :great_models do
    Given "a great_model" do
      GreatModel.create
    end
  end
  
Run the example again:
$ script/spec—format specdoc spec/steps/great_step_spec.rb

That’s it for a very first introduction.

While this great_step thing is not too useful, it is (for me anyway) quite convenient to have a bdd tool at hand when it comes to implementing more involved GWT-Steps.

‘Real World’-like example coming soon …

Crafting Rspec Steps with step_eval and DRYing them with a Helper
Part 2
Part 3

 
  1. Sebastian:
    Yeah, that looks interesting. One quick question: Why is the first failing example red and the second some kind of purple?

Sorry, comments are closed for this article.

Blog