在使用 rspec 进行测试之前如何加载 Seed_fu 装置?

发布于 2024-12-26 10:43:26 字数 788 浏览 4 评论 0原文

我正在尝试设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执笏见 2025-01-02 10:43:26

看来您可以通过简单调用 SeedFu.seed 来直接访问播种器。

这样做的优点是确保种子以正确的顺序运行,如果需要,您可以传递特定夹具路径或过滤选项的参数 - 例如,我可以看到使用对仅测试夹具有用的特定路径。

这是seed-fu 方法:

# Load seed data from files
# @param [Array] fixture_paths The paths to look for seed files in
# @param [Regexp] filter If given, only filenames matching this expression will be loaded
def self.seed(fixture_paths = SeedFu.fixture_paths, filter = nil)
  Runner.new(fixture_paths, filter).run
end

我不知道这个方法是否要公开使用,但至少现在它似乎很适合我的使用。

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:

# Load seed data from files
# @param [Array] fixture_paths The paths to look for seed files in
# @param [Regexp] filter If given, only filenames matching this expression will be loaded
def self.seed(fixture_paths = SeedFu.fixture_paths, filter = nil)
  Runner.new(fixture_paths, filter).run
end

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.

后来的我们 2025-01-02 10:43:26

我想出了可以接受但不理想的解决方案。我已从 initailizers 文件中删除了对数据库的引用,并将其替换为简单的整数赋值。为了加载我的种子,我在 spec_helper.rb 中添加了这一行

Dir[Rails.root.join("db/fixtures/*.rb")].each {|file| load file }

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

Dir[Rails.root.join("db/fixtures/*.rb")].each {|file| load file }
泪痕残 2025-01-02 10:43:26

您可以在开始之前在 rspec 测试中运行此命令:

`rake db:seed_fu`

`rake db:seed_fu FILTER=locales`

在测试之后

`rake db:reset`

注意魔术引号 - ``

示例:

describe "POST /v1/products" do
  before(:all) do
    `rake db:seed_fu FILTER=4_locales`
  end

  after(:all) do
    `rake db:reset`
  end

  ...
end

You can run this in rspec tests before start:

`rake db:seed_fu`

or

`rake db:seed_fu FILTER=locales`

and after tests

`rake db:reset`

Note the magic quotes - ``

Example:

describe "POST /v1/products" do
  before(:all) do
    `rake db:seed_fu FILTER=4_locales`
  end

  after(:all) do
    `rake db:reset`
  end

  ...
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文