[英]How to print the Model.create calls made by FactoryGirl.create?
Is there any way to print the ActiveRecord operations called by FactoryGirl.create? 有没有办法打印FactoryGirl.create调用的ActiveRecord操作?
Say we have a factory :foo
of model Foo
. 假设我们有一个工厂:foo
模型Foo
:foo
。 Running FactoryGirl.create(:foo)
, creates a database entry for the model Foo
and some other entries for different models, say Bar
, Car
, etc. 运行FactoryGirl.create(:foo)
,为模型Foo
创建数据库条目,为不同的模型创建一些其他条目,比如Bar
, Car
等。
Is there any way in which we could convert this FactoryGirl.create(:foo)
command into its underlying Model.create
commands? 有没有什么方法可以将这个FactoryGirl.create(:foo)
命令转换为它的底层Model.create
命令?
For example 例如
FactoryGirl.create(:foo)
should break into 应该闯入
Foo.create(name: 'some name', ...)
Bar.create(field_value: 'asdf', ....)
Car.create(field_value: 'asdf1231', ....)
...
factory_girl calls save!
factory_girl打电话save!
when it creates a model, so you can get at least part way there by overriding save!
当它创建一个模型时,你可以通过重写save!
至少获得部分方式save!
in each of your models. 在每个模型中。 Put this in config/initializers/print_model_save.rb
: 把它放在config/initializers/print_model_save.rb
:
class ActiveRecord::Base
alias_method :original_save!, :save!
def save!
puts "#{self.class.name}.create! #{attributes.symbolize_keys.inspect}"
original_save!
end
end
That will work for simple attributes. 这适用于简单属性。 Complex attributes might require more complicated argument printing. 复杂属性可能需要更复杂的参数打印。 Try it and see. 试试看吧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.