1

I am testing my Ruby 2.6.x project with Cucumber 3.1.2.

Now I would like to include different helper modules into World, depending on the tags of the scenario, like this:

module NormalHelper
  def do_something
    "something"
  end
end

module SpecialHelper
  def do_something
    "something different"
  end
end

Before("~@special") do
  World(NormalHelper)
end

Before("@special") do
  World(SpecialHelper)
end

However, when I run cucumber, I am told

undefined method `World' for #<Cucumber::Rails::World:0x…> (NoMethodError)

Outside of the Before hook, calling World works nicely.

What am I doing wrong?

aef
  • 4,498
  • 7
  • 26
  • 44

1 Answers1

0

First of all what you are trying to do seems to me to be a really bad idea. Why do you want to do this?

Technically perhaps World doesn't exist when Before is parsed, so you can't add to it. This is just a guess, you can explore this by debugging and pausing execution just before you try to add to World. Another possibility is that World isn't accessible from the scope inside before, that might be resolved by using ::World.

Another thing to bear in my mind is that World may not be recreated for each scenario. So what you are doing wouldn't actually work anyhow, as after one special scenario and one normal scenario have run, both modules would be in World with duplicate methods with different definitions.

diabolist
  • 3,990
  • 1
  • 11
  • 15
  • As far as I know the whole point of World ist, that there is a new instance of it for each scenario, so the the steps in the scenario share a state. – aef Jul 15 '19 at 18:05
  • You are correct there is a separate world for each scenario. However once that instance is available World isn't, as self is World. You might be able to do what you want by extending self. See what self is in your hook. – diabolist Jul 16 '19 at 08:49