Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scoping a Transform in Cucumber

Tags:

ruby

cucumber

I'm working with a very large set of already existing cucumber features, and adding additional tests. For those new tests I'm also trying to use transforms to simplify repetitive tasks.

How can I add a transform without breaking already existing tests? I've already added context to the capture group, but since the context is from the same business domain as the pre-existing tests it can easily end up matching.

Is there a way to only apply a transform to certain steps?

like image 442
Chris Pitman Avatar asked Nov 04 '22 01:11

Chris Pitman


1 Answers

You could use a tag and a Before filter to set an instance variable in the World. This is then available to your Transform so that it can perform tag-specific transforms. For example, if you only wanted to Transform integers when the @hook tag is present:

Transform /(\d+)/ do |num|
  if @hook
    num.to_i
  else
    num
  end
end

Before('@hook') do
  @hook = true
end

A new World is created for each Scenario and the Before filters are called. So @hook will be reset for each Scenario.

like image 163
graza Avatar answered Nov 18 '22 11:11

graza