Cucumbers: how to use the same regular expression in different transformations?

I have the following conversion:

Transform /^"([^"]+)" Phase$/ do |name| # Returns the phase named 'name', # or raises an exception if it doesn't exist end 

which works with the step definition as follows:

 Then /("(?:[^"]+)" Phase) should do something/ do |phase| # Should fail if the specified phase doesn't exist end 

I also have the following definition of a step that uses the same "([^"]+)" Phase pattern "([^"]+)" Phase :

 Given /("([^"]+)" Phase) follows ("([^"]+)" Phase)/ do |pre, post| # Should create the specified phases end 

Here I do not want the step definition not to be performed if the indicated phases do not exist. I would like to create phases.

I would like to create a Transform that will create a phase for me to have DRY define the definitions of the steps a bit, but I cannot do this because I already have the Transform mentioned above that has exactly the same regular expression.

Basically, I would like to create a phase if this is a Given step, and fail if it is not.

Any ideas?

+7
source share
1 answer

If the regular expressions match, then you really have no way to differentiate the behavior. Determining whether you are possible at the Given stage is possible, but even if it is, it will be very well hidden magic with the ability to surprise future readers and scriptwriters ...

The easiest and most obvious way to do this is to explicitly indicate the nature of the phrase in the language of steps, then you can have 2 clearly separated conversions, for example.

 EXISTING_PHASE = Transform /^existing Phase "([^"]+)"$/ do |phase| # raise error if it doesn't exist end UNEXISTING_PHASE = Transform /^unknown Phase "([^"]+)"$/ do |phase| # create the phase if it doesn't exist end 
+4
source

All Articles