Integration Testing with cucumber and pickle

So we have the power of cucumber for our integration tests; its a cool tool.

But when we used to write stories; we would find that it hard to make useable steps; that would then relate to readable stories.

Enter “Pickle”:git://github.com/ianwhite/pickle.git it will enable you to leverage your factories or blueprints depending on your flavor
of fixture replacement, “machinist”:git://github.com/notahat/machinist.git or “factory_girl”:git://github.com/thoughtbot/factory_girl.git.

It uses an adaptor approach so if one is willing you could create your own adaptor like datamapper’s “dm-sweatshop”:git://github.com/datamapper/dm-more.git for instance;

Ok lets get things going, and lets see what this pickle can do

Before you can use pickle you will need cucumber, so get that going first

Install “pickle”:git://github.com/ianwhite/pickle.git do the gem or plugin thing; then run the generator

    script/generate pickle paths email

we get pickle steps, extra paths steps and some email steps;

If you now look at the env.rb file you will see some pickling config has been added

you can set your fixture replacement of choice and a map config.

    Pickle.configure do |config|
      config.adapters = [:machinist]
      config.map 'I', 'myself', 'me', 'my', :to => 'user: "me"'
    end

the ‘user: “me” is essential a way of naming a model creation for later reference; check pickle readme for more information.

The best way to show what this beast does is to show a feature with the pickle steps underneath the story steps….

Example

    Feature: creating a post
      In order to allow users to create a post
      As a business owner of the site
      I want users to post content so I can advertise to that content

      Scenario: user creates a post
        Given I am logged in
        
          this will use our own authentication steps which can be seen at the bottom
        
        And I am at the new post page
        
          when /^the (.+?) page$/       # translate to named route
            send "#{$1.downcase.gsub(' ','_')}_path"
        

        When I fill in "post_title" with "I like to pickle"
        And I fill in "post_body" with "Its like a sandwich that is soo much tastier"
        And I press "Create"

        Then a post should exist with title: "I like to pickle", user: me
        
          Then(/^#{capture_model} should exist(?: with #{capture_fields})?$/) do |name, fields|
            find_model(name, fields).should_not be_nil
          end
        
        And I should be at the post's page
        
          when /^#{capture_model}(?:'s)? page$/
            path_to_pickle $1
        

        And I should see "post successfully created"
        And I should see "I like to pickle"

From the feature we can see we are using a few of the path steps that we generated earlier. capture_model is a nice regex that is made available to you so you can use it in your own step definitions; along with capture_fields.

Essentially pickle will store the models that you create in the cucumber world so you can reference the model you either find or create in future steps. With the ability to also give the model an alias as well; its really nice way to keep re-using those pickle steps throughout your feature; I am sure feeling dry right now;

Anyway the readme is good enough for me to not go on too much just wanted to give you some pickle flavor to the cucumber world.

Authentication Steps


      Given(\/^#{capture_model} (?:has|have) registered$\/) do |name|
        Given "#{name} exists"
      end

      Given(/^#{capture_model} (?:has|have) registered with "(\S+)", "(.*)"$/) do |name, email, password|
        Given "#{name} exists with email: \"#{email}\", password: \"#{password}\", password_confirmation: \"#{password}\""
      end

      Given(/^#{capture_model} (?:is|am) activated( with "\S+", ".*")?$/) do |name, credentials|
        Given "#{name} has registered#{credentials}"
        created_model(name).confirm_email!
      end


      When(/^I login with "(\S+)", "(.*)"$/) do |email, password|
        When "I go to the new session page"
        fill_in 'email', :with => email
        fill_in 'password', :with => password
        click_button 'Sign in'
      end

      Given "I am logged in" do
        Given "I am activated"
        When "I login with \"#{me.email}\", \"#{me.password}\""
        Then "I should see \"Signed in successfully.\""
      end

6 Comments

RSS feed for comments on this post. TrackBack URL

  1. Paul Mckintyre — May 13, 2009

    interesting post, cheers

  2. Nick W — December 22, 2009

    Hi, this is a great overview of Pickle. The authentication steps are especially useful. However, I’m getting an undefined method ‘me’ for Cucumber::Rails::World when trying to use the pickle mappings and your authentication steps. I’m using factory girl and have a factory for ‘me’ defined as: Factory.define :me, :class => User. Any ideas?

  3. hookercookerman — December 24, 2009

    This may help

    Env.rb

    module MeHelper
    def me
    created_model(‘user: “me”‘)
    end
    end

    World(MeHelper)

  4. Nick W — December 25, 2009

    That’s exactly what I was looking for. Thanks!

  5. Stephen Boisvert — January 31, 2010

    Just a heads up that your links are all broken (you have //git prepended to all of the urls)

    http://git//github.com/ianwhite/pickle.git”

  6. nogeek — February 1, 2010

    Cheers Stephen, have updated the links :-)

Leave a comment

Preview: