在使用 rspec 进行测试之前如何加载 Seed_fu 装置?
我正在尝试设置 rspec 以在我的 Rails 应用程序中进行测试。我创建了一些示例测试并执行了 rake rspec --trace 。
在输出中,我看到此消息
** Invoke spec:models (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
** Execute db:test:load
** Invoke db:schema:load (first_time)
** Invoke environment
** Execute db:schema:load
“模式已加载,然后 rake 任务中断”,因为我试图在初始值设定项目录中的文件之一中使用数据库中的一些值。通常这些记录都在数据库中,它们都存在于 db/fixtures 目录中的 Seed_fu 文件中。
我正在寻找一种在 db:schema:load
之后执行此文件的方法。有人知道该怎么做吗?
I'm trying to setup rspec for testing in my rails application. I have created some sample test and executed rake rspec --trace
.
In output I see this messages
** Invoke spec:models (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
** Execute db:test:load
** Invoke db:schema:load (first_time)
** Invoke environment
** Execute db:schema:load
Schema gets loaded and then rake task breaks, because I'm trying to use some values from database in one of files in initializers directory. Normally those records are in the database, they are all present in seed_fu files in db/fixtures directory.
I'm looking a way to execute this files after db:schema:load
. Somebody knows how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来您可以通过简单调用
SeedFu.seed
来直接访问播种器。这样做的优点是确保种子以正确的顺序运行,如果需要,您可以传递特定夹具路径或过滤选项的参数 - 例如,我可以看到使用对仅测试夹具有用的特定路径。
这是seed-fu 方法:
我不知道这个方法是否要公开使用,但至少现在它似乎很适合我的使用。
It appears you can access the seeder directly with a simple call to
SeedFu.seed
.This has the advantage of ensuring seeds run in the proper order, and if you need to, you can pass arguments for specific fixture paths or filtering options - I could see using a specific path useful for test-only fixtures, for instance.
Here's the seed-fu method:
I have no idea if this method is meant to be used publicly, but for now at least it seems to work well for my uses.
我想出了可以接受但不理想的解决方案。我已从 initailizers 文件中删除了对数据库的引用,并将其替换为简单的整数赋值。为了加载我的种子,我在
spec_helper.rb
中添加了这一行I came up with acceptable but not ideal solution. I've removed reference to database from initailizers file and replaced it with simple integer assignment. To load my seeds I added this line in
spec_helper.rb
您可以在开始之前在 rspec 测试中运行此命令:
或
在测试之后
注意魔术引号 - ``
示例:
You can run this in rspec tests before start:
or
and after tests
Note the magic quotes - ``
Example: