在导轨控制台中加载和使用夹具
我想知道是否有办法在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您应该能够在进入控制台之前加载您的灯具。像这样:
但是,您仍然无法像在测试中那样访问您的夹具数据。这只是用您的灯具数据加载您的测试数据库。因此,您仍然需要执行以下操作:
但是,您仍然可以为此类事情创建快捷方式。您可以将任何您想要的 ruby 代码添加到 ~/.irbrc。我建议按照此处所述创建一个 .railsrc 文件。然后,您可以设置如下内容:
因此,现在您可以在控制台加载后开始引用变量“john”。顺便说一句,我链接到的帖子展示了如何设置全局 .railsrc 文件,但您可以设置它,以便您拥有每个项目的 .railsrc。或者,如果您想要一些不太花哨但易于完成的东西...只需在您的项目中创建一个 ruby 文件(可能是“shortcuts.rb”)。加载控制台后,只需执行 require 'shortcuts' 即可。
You should be able to load your fixtures prior to entering console. Like this:
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:
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:
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'.
可能会迟到...
轨道4
May be late...
Rails 4
您也可以将装置加载到您的开发数据库中:
You can load fixtures into your development database too:
所以我有类似但略有不同的需求。我想使用我现有的装置(来自我的 rspec 测试)来填充我的开发数据库。这就是我通过将新任务添加到我的 rake 文件(位于 libs/tasks/*.rake 中)来完成的:
如果将其与 db:reset 结合使用,您可以通过将其添加到 rake 来随意填充您的开发环境任务也是:
然后您可以调用 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):
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:
Then you can call rake db:reseed to populate from fixture YAML files.
可以使用 FIXTURES_DIR 变量指定备用夹具目录。该值应与测试/夹具相关。
也可以指定一组有限的装置
It's possible to specify an alternate fixture directory using the FIXTURES_DIR variable. The value should be relative to test/fixtures.
It's also possible to specify a limited set of fixtures
您可以在 Rails 3.2 控制台中加载固定装置,如下所示:
You can load a fixture in the Rails 3.2 console as follows: