Eri*_*win 85 ruby-on-rails factory-bot
我在开发环境中使用rails控制台,我想使用工厂.我怎样才能访问它们?
我试过require "FactoryGirl"哪个回归
1.9.3p393 :301 > require "FactoryGirl"
LoadError: cannot load such file -- FactoryGirl
Run Code Online (Sandbox Code Playgroud)
Ale*_*pov 174
我这样做的方式如下:
以沙盒模式在测试环境中启动rails控制台.
rails console test --sandbox
Run Code Online (Sandbox Code Playgroud)您需要这个有两个原因:
然后在控制台中:
需要FactoryBot(被称为FactoryGirl):
require 'factory_bot'
Run Code Online (Sandbox Code Playgroud)加载工厂定义:
FactoryBot.find_definitions
Run Code Online (Sandbox Code Playgroud)包括FactoryBot方法,以避免使用FactoryBot(create而不是FactoryBot.create)代替所有FB调用:
include FactoryBot::Syntax::Methods
Run Code Online (Sandbox Code Playgroud)PS对于制作gem,您可以在rails控制台中加载定义:
Fabrication.manager.load_definitions
Run Code Online (Sandbox Code Playgroud)
此外require 'faker',如果你使用它.
mut*_*amb 37
要解决此问题,请确保在您的Gemfile中指定工厂bot gem,类似于此
group :development, :test do
gem 'factory_bot_rails'
end
Run Code Online (Sandbox Code Playgroud)
然后bundle install.
这应该使FactoryBot类在开发控制台中可用.
希望这可以帮助.
Rob*_*rty 22
你需要require 'factory_bot_rails',这是Rails使用的实际宝石.该宝石将包括Factory Girl库,使FactoryGirl可用.
你可以这样做,或者在启动时更新你的Gemfile以在muttonlamb的答案中要求它.
Jac*_*cka 15
如果您希望每次启动控制台时都可用,可以将这段代码添加到 config/environments/development.rb 的顶部:
require 'factory_bot_rails'
require 'faker' # if you're also using faker gem
require 'rails/console/helpers'
Rails::ConsoleMethods.prepend(FactoryBot::Syntax::Methods)
Run Code Online (Sandbox Code Playgroud)
现在,您可以在启动控制台后立即使用内置帮助程序,例如:
company = create(:company)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32202 次 |
| 最近记录: |