Ruby on Rails:Cucumber:如何在每个场景后重置数据库?

发布于 2024-09-05 10:29:38 字数 753 浏览 1 评论 0原文

所以我现在在我的测试环境中

,在终端中, rake db:test:prepare 清除数据库...但当我从代码运行它时不会清除

我在 features/support/env.rb 中有这个:

Before do
    task :build_all do
      [ :debug, :release ].each do |t|
        $build_type = t
        Rake::Task["db:test:prepare"].reenable
        Rake::Task["db:test:prepare"].invoke
      end
    end
end

但是我的数据当我的测试完成运行时,仍保留在project_test数据库中

这是在我的database.yml中

test:
  adapter: mysql
  encoding: utf8
  database: projectname_test
  username: root
  password:

我也尝试过

db:test:purge

,并且

db:test:reset

我知道它正在使用我的测试数据库,因为我检查mySQLWorkbench,并且它将数据插入表中...但没有完成后不要删除数据(我必须手动删除它)。 当表为空时,测试用例通过

So I am in my test environment

now, in the terminial, rake db:test:prepare clears the db... but not when i run it from the code

And I have this in features/support/env.rb:

Before do
    task :build_all do
      [ :debug, :release ].each do |t|
        $build_type = t
        Rake::Task["db:test:prepare"].reenable
        Rake::Task["db:test:prepare"].invoke
      end
    end
end

But my data remains in the project_test db when my tests are done running

This is in my database.yml

test:
  adapter: mysql
  encoding: utf8
  database: projectname_test
  username: root
  password:

Ive also tried

db:test:purge

and

db:test:reset

and I know that it is using my test db, because I check mySQLWorkbench, and it inserts data into the tables... but doesn't delete the data when its done (I have to delete it manually).
When the tables are empty, the test cases pass

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

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

发布评论

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

评论(3

虚拟世界 2024-09-12 10:29:38

你应该使用

begin
  require 'database_cleaner'
  require 'database_cleaner/cucumber'
  DatabaseCleaner.strategy = :truncation

rescue NameError
  raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."

end

Before do
  DatabaseCleaner.start
end

After do |scenario|
  DatabaseCleaner.clean
end

You should use

begin
  require 'database_cleaner'
  require 'database_cleaner/cucumber'
  DatabaseCleaner.strategy = :truncation

rescue NameError
  raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."

end

Before do
  DatabaseCleaner.start
end

After do |scenario|
  DatabaseCleaner.clean
end
水晶透心 2024-09-12 10:29:38

Cucumber 中的场景与 RSpec 中的测试一样,在事务块中运行,并在场景完成后回滚。数据库中不应该存在的任何数据都可能是其他数据遗留下来的。尝试清除您的数据库。

Scenarios in Cucumber, like tests in RSpec, are run in transactions blocks and are rolled back once the scenario is done. Any data that's in the database that shouldn't be there is probably left over from something else. Try purging your database.

吖咩 2024-09-12 10:29:38

我不知道这一点,但是您是否尝试过动态删除并重新创建数据库?

Idk about this, but have you tried dropping and recreating the database on the fly?

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