在导轨控制台中加载和使用夹具

发布于 2024-11-29 10:56:00 字数 260 浏览 5 评论 0原文

我想知道是否有办法在 Rails 控制台中加载和/或使用夹具。实际上,我想从我的装置 users.yml 创建一个用户来进行一些测试,而不必经历 User.new(:name = " 的所有“痛苦” John", :email = "..") 每次。
我目前处于测试环境(rails c RAILS_ENV=test)。

如果这不是一个好的做事方式,请说出来。我是 Rails 新手,所以我来这里学习:)

I wonder if there's a way to load and/or use fixture in rails console. Actually, I'd like to create a user from my fixture users.yml to do some testing without having to go through all the "pain" of doing User.new(:name = "John", :email = "..") each time.
I am currently in test environment (rails c RAILS_ENV=test).

If it's not a good way to do things, please say it. I'm new to Rails so I'm here to learn :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

永言不败 2024-12-06 10:56:00

您应该能够在进入控制台之前加载您的灯具。像这样:

RAILS_ENV=test bin/rails db:fixtures:load
RAILS_ENV=test bin/rails console

但是,您仍然无法像在测试中那样访问您的夹具数据。这只是用您的灯具数据加载您的测试数据库。因此,您仍然需要执行以下操作:

user = User.find_by(name: "John")

但是,您仍然可以为此类事情创建快捷方式。您可以将任何您想要的 ruby​​ 代码添加到 ~/.irbrc。我建议按照此处所述创建一个 .railsrc 文件。然后,您可以设置如下内容:

john = User.find_by(name: "John")

因此,现在您可以在控制台加载后开始引用变量“john”。顺便说一句,我链接到的帖子展示了如何设置全局 .railsrc 文件,但您可以设置它,以便您拥有每个项目的 .railsrc。或者,如果您想要一些不太花哨但易于完成的东西...只需在您的项目中创建一个 ruby​​ 文件(可能是“shortcuts.rb”)。加载控制台后,只需执行 require 'shortcuts' 即可。

You should be able to load your fixtures prior to entering console. Like this:

RAILS_ENV=test bin/rails db:fixtures:load
RAILS_ENV=test bin/rails console

However, you still won't be able to access your fixture data like you would in a test. This simply loads your test database with your fixtures data. So you'd still have to do something like:

user = User.find_by(name: "John")

But, you can still create shortcuts for this sort of thing. You can add any ruby code you'd like to your ~/.irbrc. I suggest creating a .railsrc file as described here. You can then set up things like:

john = User.find_by(name: "John")

So now you can just start referring to the variable 'john' after console loads. Incidentally, the post I linked to shows how to set up a global .railsrc file, but you could set it up so that you had a per project .railsrc. Or, if you want something a little less fancy, but easy to do... just create a ruby file in your project (maybe 'shortcuts.rb'). After console is loaded, just do a require 'shortcuts'.

梦初启 2024-12-06 10:56:00

可能会迟到...
轨道4

require 'active_record/fixtures'
ActiveRecord::FixtureSet.create_fixtures(Rails.root.join('test', 'fixtures'), 'users')

May be late...
Rails 4

require 'active_record/fixtures'
ActiveRecord::FixtureSet.create_fixtures(Rails.root.join('test', 'fixtures'), 'users')
客…行舟 2024-12-06 10:56:00

您也可以将装置加载到您的开发数据库中:

$ rake db:fixtures:load
$ rails c
> require 'active_record/fixtures'
> john = User.find ActiveRecord::FixtureSet.identify('john')

You can load fixtures into your development database too:

$ rake db:fixtures:load
$ rails c
> require 'active_record/fixtures'
> john = User.find ActiveRecord::FixtureSet.identify('john')
热血少△年 2024-12-06 10:56:00

所以我有类似但略有不同的需求。我想使用我现有的装置(来自我的 rspec 测试)来填充我的开发数据库。这就是我通过将新任务添加到我的 rake 文件(位于 libs/tasks/*.rake 中)来完成的:

task d_populate: :environment do
  require 'active_record/fixtures'
  fixtures_dir = File.join(Rails.root, '/spec/fixtures') #change '/spec/fixtures' to match your fixtures location
  Dir.glob(File.join(fixtures_dir,'*.yml')).each do |file|
  base_name = File.basename(file, '.*')
  puts "Loading #{base_name}..."
  ActiveRecord::Fixtures.create_fixtures(fixtures_dir, base_name)
  end
end

如果将其与 db:reset 结合使用,您可以通过将其添加到 rake 来随意填充您的开发环境任务也是:

task reseed: [:environment, 'db:reset', 'db:d_populate']

然后您可以调用 rake db:reseed 从固定装置 YAML 文件中填充。

So I had a similar but slightly different need. I wanted to use my existing fixtures (from my rspec test) to populate my development database. This is how I did it by adding a new task to my rake file (located in libs/tasks/*.rake):

task d_populate: :environment do
  require 'active_record/fixtures'
  fixtures_dir = File.join(Rails.root, '/spec/fixtures') #change '/spec/fixtures' to match your fixtures location
  Dir.glob(File.join(fixtures_dir,'*.yml')).each do |file|
  base_name = File.basename(file, '.*')
  puts "Loading #{base_name}..."
  ActiveRecord::Fixtures.create_fixtures(fixtures_dir, base_name)
  end
end

If you combine this with a db:reset you can populate your development environment at will by adding this to your rake task as well:

task reseed: [:environment, 'db:reset', 'db:d_populate']

Then you can call rake db:reseed to populate from fixture YAML files.

衣神在巴黎 2024-12-06 10:56:00

可以使用 FIXTURES_DIR 变量指定备用夹具目录。该值应与测试/夹具相关。

$ rake db:fixtures:load RAILS_ENV=test FIXTURES_DIR='../../spec/fixtures'

也可以指定一组有限的装置

$ rake db:fixtures:load RAILS_ENV=test FIXTURES_DIR='../../spec/fixtures' FIXTURES=users,user_roles

It's possible to specify an alternate fixture directory using the FIXTURES_DIR variable. The value should be relative to test/fixtures.

$ rake db:fixtures:load RAILS_ENV=test FIXTURES_DIR='../../spec/fixtures'

It's also possible to specify a limited set of fixtures

$ rake db:fixtures:load RAILS_ENV=test FIXTURES_DIR='../../spec/fixtures' FIXTURES=users,user_roles
中二柚 2024-12-06 10:56:00

您可以在 Rails 3.2 控制台中加载固定装置,如下所示:

require 'active_record/fixtures'

ActiveRecord::Fixtures.create_fixtures FIXTURE_PATH_HERE, MODEL_NAME_HERE

You can load a fixture in the Rails 3.2 console as follows:

require 'active_record/fixtures'

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