Rspec 集成测试不清理数据库
每次集成测试后数据库都不会被清理。该值保留在数据库中。
我应该有一个选择来实现这一点吗?
谢谢
The database is not being cleaned after each integration test. The value stays in the database.
Is there an option I should have to make this happen?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为 https://github.com/bmabey/database_cleaner 就是您所需要的。
I think https://github.com/bmabey/database_cleaner is what you need.
对于任何使用
before(:all)
挂钩的人,请注意这些挂钩是在打开与固定装置关联的事务之前执行的。这意味着由 before(:all) 挂钩创建的任何数据都不会被事务固定装置回滚。您可以RSpec 文档阅读更多内容。我只是想提一下这一点,因为我被它咬住了,我最初的本能是跳到数据库清理器(最终不需要它并最终不起作用)。
For anyone using
before(:all)
hooks, be aware that these hooks are executed before the transaction associated to the fixture is opened. This means that any data created by before(:all) hooks will not be rolled back by transactional fixtures. You can read more in the RSpec documentation.I just wanted to mention this because I was bit by it and my initial instinct was to jump to Database Cleaner (which wound up not being needed and eventually not working).
如何在不运行 rake spec 的情况下为 Rails rspec 测试准备测试数据库?
您可能会对我的答案感兴趣。这是一个很好的解决方案。对于您的情况,您可能需要类似的东西
How do I prepare test database(s) for Rails rspec tests without running rake spec?
My answer there might be of interest to you. it's a nice solution. For your case, you would probably need something like
在这里查看教程:http://railscasts.com/episodes/257-request -specs-and-capybara
它描述了除 Rspec 和 Capybara 之外的数据库清理器
Look here for a tutorial: http://railscasts.com/episodes/257-request-specs-and-capybara
It describes Database Cleaner besides Rspec and Capybara
您需要 DatabaseCleaner,但您可能会发现
:truncation
策略有点太慢,无法始终运行。它实际上只适用于集成测试,因此您可以这样做:显然,这只适用于直接使用 RSpec 进行集成测试而不是使用 Cucumber 进行集成测试。
You want DatabaseCleaner, but you may find that the
:truncation
strategy is a bit too slow to run all the time. It's really only necessary for integration tests, so you can do this:Obviously, this only applies if you're doing integration tests with RSpec directly vs. doing them with Cucumber.
有两种方法可以实现此目的:
如果您选择选项 1:在规范文件的顶部,之后:
添加:
这将在每个示例之后回滚事务。
2.如果要全局配置,那么,在spec_helper.rb中
There are two ways to accomplish this:
If you opt for option 1: At the top of the spec file, after:
Add:
That will roll back the transactions after each example.
2.If you want to configure it globally, then, in the spec_helper.rb