想要在运行 Cucumber 之前加载种子数据

发布于 2024-10-21 23:09:26 字数 257 浏览 1 评论 0原文

我希望黄瓜在开始测试之前将我的种子数据加载到“db/seeds.rb”中。不是在每个场景或功能之前,而是在运行测试之前仅一次。 而且在每个场景之后,种子必须保留在数据库中。

这可能吗?

我尝试创建一个文件“features/support/seed_data.rb”并要求其中包含我的 db/seeds.rb,但似乎该文件根本没有加载。我试图在 env.rb 中要求我的种子 - 没有影响。

请问有人可以建议我解决方案吗?

提前致谢!

I want cucumber to load my seed data in "db/seeds.rb" before starting to test. Not before each scenario or feature, but only once before running the tests.
And also after each scenario, the seeds must remain in db.

Is that possible?

I've tried creating a file "features/support/seed_data.rb" and requiring my db/seeds.rb in there, but it seems that file is not loaded at all. I tried to require my seeds in env.rb - no affect.

Please, can anybody suggest me the solution?

Thanks in advance!

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

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

发布评论

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

评论(2

嘦怹 2024-10-28 23:09:26

在 support/hooks.rb 文件中创建一个 before 钩子,如下所示:

Before('@load-seed-data') do
  load File.join(Rails.root, 'db', 'seeds.rb')
end

在场景之前的测试中,像这样调用钩子:

@load-seed-data @US49
Scenario: This is a scenario that needs seed data.
Given...

Create a before hook in your support/hooks.rb file which looks like this:

Before('@load-seed-data') do
  load File.join(Rails.root, 'db', 'seeds.rb')
end

In your test before the scenario, call the hook like this:

@load-seed-data @US49
Scenario: This is a scenario that needs seed data.
Given...
眸中客 2024-10-28 23:09:26

如何从 seeds.rb 文件中提取代码并将其粘贴到 AfterConfiguration 块中的 hooks.rb 中?

AfterConfiguration do |config|
  # Your code from seeds.rb
end

应该在运行期间、配置 Cucumber 后立即调用一次。至少这样做,您可以确定是否只是在包含种子文件时遇到问题。另一个想法是采用 seeds.rb 并将其作为模块直接粘贴到支持目录中,然后从 AfterConfiguration 调用它:

# db_seeds.rb
module DbSeeds 

  def seed_db
    # Your Code
  end

end

World(DbSeeds)

#hooks.rb
AfterConfiguration do |config|
  seed_db
end

How about pulling the code from your seeds.rb file and sticking it in hooks.rb in an AfterConfiguration block?

AfterConfiguration do |config|
  # Your code from seeds.rb
end

That should be called once during a run, right after cucumber is configured. At least doing it this way, you can determine if you just have an issue with including your seeds file or not. Another idea would be to take seeds.rb and stick it directly inside the support directory as a module and then call it from AfterConfiguration:

# db_seeds.rb
module DbSeeds 

  def seed_db
    # Your Code
  end

end

World(DbSeeds)

#hooks.rb
AfterConfiguration do |config|
  seed_db
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文