测试 Factory Girl

可用于构建 record 对象的方法

构建单个对象:

build
create
attributes_for
build_stubbed

使用举例:

build(:completed_order)

create(:post) do |post|
  create(:comment, post: post)
end

attributes_for(:post, title: "I love Ruby!")

build_stubbed(:user, :admin, :male, name: "John Doe")

构建多个对象:

build_list
create_list
attributes_for_list
build_stubbed_list

使用举例:

链接 FactoryGirl Syntax Methods

Getting Started

Linting Factories

类方法:

Defining factories

类方法:

实例方法:

(定义 factory.)

Lazy Attributes

定义属性时,大括号 {} 的使用。

Aliases

调用 factory 时,可选参数 :aliases 的使用(给 factory 起外号)。

Dependent Attributes

{} 大括号里面求值(拼接字符串)。

Transient Attributes

(在定义 factory 的代码内同时定义方法,之后使用到)

Associations

以及它的可选参数 :factory:strategy

(关联对象的 factory.)

Inheritance

嵌套使用 factory 方法。

或 factory 的可选参数 :parent

(继承已有的 factory.)

Sequences

(按顺序生成 factory 的属性内容)

Traits

定义:用 trait 进行定义。

调用:factory 的可选参数 :traits 或直接调用。

(避免重复代码)

Callbacks

默认已经有:

(创建 factory 时的回调)

自定义:

Modifying factories

类方法:

(更改已有 factory. 场景:在 rails c 里检验)

Building or Creating Multiple Records

(批量创建 factory 对象)

Custom Construction

需要自定义相关类及方法。

(Model 里使用了 initialize 并设置了实例变量)

Custom Strategies

类方法:

需要自定义相关类及方法。

(自定义 factory 对象的生成规则)

Custom Callbacks

使用"Custom Strategies"会自动添加上述默认的 Callbacks.

Custom Methods to Persist Objects

(重新定义并调用"保存方法";创建 factory 对象的时候跳过"保存")

重新加载 factory

类方法:

链接 Getting Started

最后更新于

这有帮助吗?