简体   繁体   English

黄瓜:在所有情况下钩子运行一次之前

[英]Cucumber: Before hook run only once for all scenarios

I have a scenario outline with multiple scenarios. 我有一个包含多个场景的场景大纲。 I'd like my Before hook to run only once so I can bootstrap the ActiveRecord objects I need to run against all of the scenarios. 我希望我的Before钩子只运行一次所以我可以引导我需要针对所有场景运行的ActiveRecord对象。 The problem is if I use 问题是如果我使用

Before do
    # my code here
end

This will execute before each Scenario. 这将在每个场景之前执行。 Is there anyway to run it once for the entire Outline? 反正有没有为整个大纲运行一次?

I think if you simply create the objects in a file in features/support they will be persisted: 我想如果您只是在功能/支持的文件中创建对象,它们将被保留:

ImportantThing.create(:name => "USEFUL THING")

This is because before every Scenario Cucumber will start a database transaction and then rollback to its prior status, which should contain the objects you've loaded. 这是因为在每个场景之前,Cucumber将启动数据库事务,然后回滚到其先前状态,该状态应该包含您已加载的对象。

I had the same problem, where I needed to create a subscriber manager once for all of my event logging tests. 我遇到了同样的问题,我需要为所有事件记录测试创建一次订阅者管理器。 If I just used a before hook or a regular step (eg a Given), the manager would be created before each scenario. 如果我只使用了前钩子或常规步骤(例如,给定),则在每个场景之前创建管理器。

My solution was ultimately to use a tagged before hook on my first scenario. 我的解决方案最终是在我的第一个场景中使用挂钩前标记。

Before('@first_logging_scenario') do
  # do something useful
end

To shutdown my manager, I used a tagged After hook with my last scenario 为了关闭我的经理,我在上一个场景中使用了标记的After hook

After('@last_logging_scenario') do
  # do something useful
end

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

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