适用于所有场景的黄瓜步骤
我有大约 30 个场景,其中一个场景都要求此步骤位于 Background
的顶部:
Given I have an account:
| name | path |
| ticketee | ticketee |
对于不需要此步骤的场景,它存在或不存在并不重要,因为这是创建帐户的功能。我只需使用不同的帐户名和路径即可。
现在,我在想,与其将其放入每个功能文件中 29 次,不如使用 Cucumber 中的 Before
方法,这意味着将文件放入 features/support/create_account 中。 rb
会有这样的代码:
Before do
steps(%Q{
Given I have an account:
| name | path |
| ticketee | ticketee |
})
end
唯一的缺点是,它提取了一些人认为属于特征中非常难以追踪的位置的内容,并且可能不是标准的。但另一方面,它节省了相当多的重复。
我应该怎么办?
I have around 30 scenarios that all bar one require this step to be at the top of the Background
:
Given I have an account:
| name | path |
| ticketee | ticketee |
For the one that doesn't require this step it is not important that it exists or doesn't exist, because it's the feature for creating accounts. I can simply use a different account name and path for this.
Now, I was thinking rather than putting this in every single feature file 29 times that I could make use of the Before
method in Cucumber which would mean placing a file in features/support/create_account.rb
which would have this code:
Before do
steps(%Q{
Given I have an account:
| name | path |
| ticketee | ticketee |
})
end
The only downside from this is that it extracts what some would think belongs in the feature to a very difficult-to-track-down location and is probably not standard. But on the other hand, it saves quite a lot of repetition.
What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会使用标记挂钩,例如
@with_ticketee_account
。它仍然带来了一点重复,但它使场景的背景比完全隐藏更明显。如果您想做到这一点,那么您只需标记一个奇怪的场景,也许可以创建一个标记钩子,例如
@without_ticketee_account
,它设置一个变量,您的通用过滤器可以在创建票证帐户之前检查该变量。I'd use a tagged hook such as
@with_ticketee_account
. It still brings a bit of repetition, but it makes the background of the scenario more obvious than having it completely hidden.If you wanted to make it so you only have to tag the one odd scenario, maybe create a tagged hook such as
@without_ticketee_account
which sets a variable which your generic before filter could check for before creating the ticketee account.恕我直言,标记钩子是个好主意。
另一个想法是制作一行而不是三行。也许:
它做了所有相同的事情,但也很简洁。我从未将这些表格与黄瓜一起使用,因为我通常可以在一行上制作可读性更好的内容。
Tagged hook is IMHO a good idea.
Another Idea is to make that one line instead of three. perhaps:
which does all the same stuff, but is succinct as well. I have never used those tables with cucumber because I can usually make stuff on one line that reads better.
我会继续将其提取到 Before 块中,就像您建议的那样或使用工厂(例如factory_girl)。
I'd go ahead and extract it into the Before block like you have proposed or use a factory (e. g. factory_girl).