Cucumber:如何在不同的转换中使用相同的正则表达式?

Cucumber:如何在不同的转换中使用相同的正则表达式?,cucumber,transform,Cucumber,Transform,我有以下转变: Transform /^"([^"]+)" Phase$/ do |name| # Returns the phase named 'name', # or raises an exception if it doesn't exist end 这适用于步骤定义,如下所示: Then /("(?:[^"]+)" Phase) should do something/ do |phase| # Should fail if the specified phase do

我有以下转变:

Transform /^"([^"]+)" Phase$/ do |name|
  # Returns the phase named 'name',
  # or raises an exception if it doesn't exist
end
这适用于步骤定义,如下所示:

Then /("(?:[^"]+)" Phase) should do something/ do |phase|
  # Should fail if the specified phase doesn't exist
end
我还有以下步骤定义,它使用相同的
“([^”]+)”阶段
模式:

Given /("([^"]+)" Phase) follows ("([^"]+)" Phase)/ do |pre, post|
  # Should create the specified phases 
end
在这里,如果指定的阶段不存在,我不希望步骤定义失败。我希望改为创建阶段

我想创建一个转换,为我创建一个阶段,使步骤定义干涸一点,但我不能这样做,因为我已经有了上面提到的转换,它具有完全相同的regexp

基本上,如果阶段是给定的步骤,我会创建该阶段,如果不是,则会引发失败


有什么想法吗?

如果regexp是相同的,那么你真的没有选择来区分行为。确定你是否在给定的
步骤中可能是可能的,但即使如此,这将是一种非常隐蔽的魔法,有可能让未来的读者和场景作者感到惊讶

要做到这一点,最简单、最能揭示意图的方法是在step语言中明确说明短语的性质,然后您可以有两个明确分开的转换,例如:

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

+我只想看片名。那将是一部好电影。