Factory Girl是一个方便的rails框架,可以轻松创建测试模型实例.
factory_girl允许您快速定义每个模型的原型,并询问具有对手头测试很重要的属性的实例.
一个例子(也来自主页):
Factory.sequence :email do |n|
"somebody#{n}@example.com"
end
# Let's define a factory for the User model. The class name is guessed from the
# factory name.
Factory.define :user do |f|
# These properties are set statically, and are evaluated when the factory is
# defined.
f.first_name 'John'
f.last_name 'Doe'
f.admin false
# This property is set "lazily." The block will be called whenever an
# instance is generated, and the return value of …Run Code Online (Sandbox Code Playgroud)