简体   繁体   English

如何提前退出Ruby Cucumber Step定义?

[英]How to exit out of Ruby Cucumber Step definition early?

I don't know why this is so difficult. 我不知道为什么这么难。 I've tried several things, but for some reason this Ruby Cucumber code either continues onto the next code in the block, or I get something like "unexpected return (LocalJumpError)". 我已经尝试了几件事,但由于某种原因,这个Ruby Cucumber代码要么继续到块中的下一个代码,要么得到类似“意外返回(LocalJumpError)”的东西。 Is this something obvious that I can't find the answer to? 这是显而易见的,我找不到答案吗?

I have a workaround by just wrapping all the code in an "if condition == false", but is there a better way? 我有一个解决方法,只需将所有代码包装在“if condition == false”中,但是有更好的方法吗?

When(/^I create scenarios$/) do
   if condition == true
     #exit, return, next, break ????
   end
   #code i don't want to execute if condition matches
end

我相信您希望通过使用'next'提前退出该区块。

next if condition

The issue your having with the code sample you have given is that the code you don't want to execute isn't part of your if block. 您使用代码示例的问题是您不想执行的代码不是if块的一部分。

What happens the way you have written it, is it will check the if condition, then execute based on what it finds. 你编写它的方式会发生什么,它会检查if条件,然后根据它找到的内容执行。 Then, it moves to the next line of code, which is your code you don't want to run. 然后,它移动到下一行代码,这是您不想运行的代码。

My suggestion, is to try something like this, 我的建议是尝试这样的事情,

When(/^I create scenarios$/) do
   if condition == true
   else
     #code i don't want to execute if condition matches
   end
end

What will happen here, is it will check the if condition, and if it is true, the code will do nothing but SKIP the else step because it met the if condition. 这里会发生什么,它会检查if条件,如果是真的,代码除了跳过else步骤之外什么都不做,因为它符合if条件。 When it checks the if condition, if it comes back false, then it will move on to the else step, and execute that code. 当它检查if条件时,如果它返回false,那么它将继续执行else步骤,并执行该代码。

I cant promise this is the BEST way to write this code, but this is simple and should suffice to solve the problem 我不能保证这是编写此代码的最佳方式,但这很简单,应该足以解决问题

Because you want nothing to happen if the condition is true, I would recommend using unless in the step definition. 因为如果条件为真,你不希望发生任何事情,我建议unless在步骤定义中使用。

When(/^I create scenarios$/) do
  unless condition == true
     #code you want to execute when the condition is false
  end
end

If condition == true , the flow will just move on to the next step. 如果condition == true ,则流程将继续进行下一步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM