在 Rails 3 中,如何确保在模型加载之前将枚举表加载到测试数据库中?
使用 ActiveSupport::TestCase。我认为这是旧的 Test::Unit。
我有几个代表枚举值的表。这些永远不会改变,我想在活动记录查找器的类范围内使用它们的数据。
这会导致测试出现问题,因为模型类会在固定装置之前加载,并且固定装置会在测试之间回滚。我无法将枚举表复制到固定装置中,因为加载模型时数据尚未加载。
有没有办法在模型加载之前和夹具事务开始之前将测试数据库置于初始状态?
Using ActiveSupport::TestCase. I think that's the old Test::Unit.
I have several tables that represent enumerated values. These never change and I would like to use their data in class scope for activerecord finders.
This causes a problem with tests because model classes load before fixtures and fixtures are rolled back between tests. I can't copy the enum tables into a fixture because the data will not yet be loaded when models load.
Is there a way to bring the test database to an initial state before models load and before the fixture transaction begins?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为您的测试定义一个设置方法怎么样?
或者使用 db/seeds.rb 为数据库播种?
How about defining a setup method for your tests?
Or seeding the database using db/seeds.rb?
如果它们永远不会改变,您可以将创建语句直接放入迁移中,以便在重建表时创建它们 - 老实说似乎有点混乱,但应该可以工作。
If they never ever change you could put the create statements directly into your migrations so they are created when the tables are rebuilt - seems a bit messy to be honest though but should work.