在 Rails 中测试 seeds.rb
在更新生产之前,我需要确保使用种子执行的所有操作都正常工作。
如何使用 RSpec 测试 seeds.rb?
Before updating production, I need to ensure, that all manipulations, performed with seeds are working correctly.
How do you test seeds.rb with RSpec?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
种子旨在首先在空数据库上运行,以正确给出正确的状态。如果初始状态需要更改(例如域表),您必须相应地调整种子,以便它可以添加不存在的元素或更改现有元素。实现这一目标的一个好方法是执行以下操作:
在运行测试之前,我们还加载种子,因此很容易创建一个规范来验证所需的数据是否存在(以防您不信任它) )。
如果您需要操作现有数据,首选方法是使用迁移。我通常不会编写迁移规范,而是在我的开发数据库和生产数据库的副本上测试它们(在实际生产之上运行它之前)。
希望这有帮助。
A seed is intended to be run first on an empty database, to correctly give the correct state. If that initial state needs to change (e.g. domain tables) you have to adapt the seed accordingly that it can add non-existing elements or change existing elements. A good way to achieve this is to do something like:
Before running the tests, we also load the seeds, so then it is easy to create a spec that will verify that the needed data is there (in case you wouldn't trust it).
If you need to manipulate existing data the preferred way is to use a migration. I generally do not write specs for migrations, but test them on my development database, and on a copy of my production database (before running it on top of actual production).
Hope this helps.