Cucumber:在钩子之前,所有场景仅运行一次

发布于 2024-09-29 01:18:36 字数 180 浏览 1 评论 0原文

我有一个包含多个场景的场景大纲。我希望 Before 挂钩仅运行一次,以便我可以引导针对所有场景运行所需的 ActiveRecord 对象。问题是如果我使用

Before do
    # my code here
end

这将在每个场景之前执行。有没有办法对整个大纲运行一次?

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. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜光 2024-10-06 01:18:36

我认为,如果您只是在 features/support 中的文件中创建对象,它们将被持久化:

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

这是因为在每个场景 Cucumber 之前都会启动一个数据库事务,然后回滚到其先前的状态,其中应该包含您已加载的对象。

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.

雄赳赳气昂昂 2024-10-06 01:18:36

我遇到了同样的问题,我需要为所有事件日志记录测试创建一次订阅者管理器。如果我只使用 before 挂钩或常规步骤(例如给定),则将在每个场景之前创建管理器。

我的解决方案最终是在我的第一个场景中使用标记的 before 钩子。

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

为了关闭我的经理,我在最后一个场景中使用了标记的 After 钩子

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

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 (e.g. 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('@last_logging_scenario') do
  # do something useful
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文