Ruby on Rails:Cucumber:如何在每个场景后重置数据库?
所以我现在在我的测试环境中
,在终端中, 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你应该使用
You should use
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.
我不知道这一点,但是您是否尝试过动态删除并重新创建数据库?
Idk about this, but have you tried dropping and recreating the database on the fly?