aslakhellesoy / cucumber

BDD that talks to domain experts first and code second

Home | Edit | New

A Whole New World

Cucumber runs scenarios in a World. By default, the World is just an instance of Object. If you want to add any behaviour to the world, perhaps some helper methods, or logging, or whatever you can do this in support/env.rb:

class CustomWorld
  def a_helper
    ...
  end
end

World do
  CustomWorld.new
end

Now you can call a_helper from your step definitions. Note that every scenario is run in a separate instance of the world, so there is no implicit state-sharing from scenario to scenario.

You can also include modules in your World:

module MyHelper
  def some_other_helper
    ...
  end
end

class CustomWorld
  include MyHelper

  def a_helper
    ...
  end
end

World do
  CustomWorld.new
end

Several other frameworks such as Webrat or RSpec have modules that provide special methods that you can include in your World this way.

If you don’t want to define your own World class (and just use the default Object instances), you can still include modules in your World instances without polluting Object with a global include:

module MyHelper
  def some_other_helper
    ...
  end
end

module MyOtherHelpers
  def helper_b
    ...
  end
end

World(MyHelper, MyOtherHelpers)

This will extend each new World object with those modules.

If you use Ruby on Rails, there is already a World set up for you, so you will get an instance of Cucumber::Rails::World, which is a subclass of ActionController::IntegrationTest. This gives you access to a lot of Rails’ helper methods.

Related blog posts

Last edited by aslakhellesoy, Fri Oct 09 07:19:45 -0700 2009
Home | Edit | New
Versions: