文档章节

FactoryGirl (Bot) - create vs build vs build_stubb

Kolosek
 Kolosek
发布于 2018/02/09 17:08
字数 328
阅读 19
收藏 0

If you are new to writing RSpec tests in Rails using FactoryGirl (bot), you can ask yourself what is the difference between createbuild and build_stubbed. In this article, we will do our best to help you understand them better.

Let's say we are testing a valid profile creation:

FactoryBot.define do
  factory :profile do
    user
    sequence(:name) { |n| "Name#{n}" }
  end
end

describe Profile, type: :model do
  it 'creates valid profile' do
    expect(FactoryBot.create(:profile)).to be_valid 
  end
end

FactoryBot.create(:profile) Will create profile object and all association for it. They will all be persisted in the database. Also,  it will trigger both model and database validations. Example test performance with create is:

create performance image

Note: after (: build) and after (: create) will be called after the factory is saved. Also, before (: create) will be called before the factory is saved.

On the other hand,  FactoryBot.build(:profile) will not save the object, but will still make requests to a database if the factory has associations. It will trigger validations only for associated objects. Example test performance with build  is:

build performance image

Note: after (: build) will be called after the factory is built.

FactoryBot.build_stubbed(:profile) It creates and assigns attributes to an object to make it behave like an instantiated object. It provides a fake id and created_at. Associations, if any, will be created via build_stubbed too. It will not trigger any validations. Example test performance with  build_stubbed  is:

build performance image

Note: after (: stub) will be called after the factory is stubbed via buildstubbed.

Keep in mind that this was a very simple test example. Better performance results are expected with more complex test suites.

Hopefully, it helped you better understand the differences between the ways to instantiate the objects when testing with FactoryBot.

Be sure to check our take on  Rspec let vs before and explanation on FactoryGirl associations. Also if you're totally new to RSpec, we have a guide to set it up.

Originally published on kolosek.com.

© 著作权归作者所有

下一篇: React JSX
Kolosek
粉丝 0
博文 29
码字总数 20461
作品 0
塞尔维亚
CEO
私信 提问
学习札记——Rspec+factory_girl进行复杂模型测试

本文主要介绍怎么使用Rspec+factory_girl进行复杂模型测试, 首先介绍下本人使用的模型机构 end 可以看出,我使用的是一个自关联表,通过自己:parent_id这个键将本表自己关联起来, 现在介绍...

JackSongBlack
2014/02/13
0
0
FactoryGirl以及Faker的使用

(1)Rails默认提供了快速生成示例数据的工具,叫做"固件"。固件是一个YAML格式的文件,可以用来生成示例数据。例如: contacts.yml aaron: firstname: "Aaron" lastname: "Sumner" email: "......

casoc
2015/04/09
655
0
Visual Studio Build Tools now include the VS2017 a

Visual Studio Build Tools now include the VS2017 a Visual C++ Team Blog2017-11-028 阅读 toolsincludestudiobuild The Visual C++ Build tools have been a huge success since we int......

Visual C++ Team Blog
2017/11/02
0
0
FLTK 1.1.10 VS2010 Configuration 配置

Download FLTK 1.1.10 at here. Download VS2010 Download CMake 2.8.12 I assume you've already installed VS2010 and CMake 2.8.12 correctly. Compile the VTK: Start CMake 2.8.12, fil......

机器的心脏
2017/12/15
0
0
VTKMY 3.3 VS 2010 Configuration 配置

Download VTKMY 3.3 Download VS2010 Download CMake 3.2.0 I assume you've already installed VS2010 and CMake 3.2.0 correctly. Compile the VTKMY: Start CMake 3.2.0, fill the source......

机器的心脏
2017/12/15
0
0

没有更多内容

加载失败,请刷新页面

加载更多

idea修改新的git地址

我们在项目变动中通常会遇到更换git地址情况,这里介绍一个在idea项目中简单更换git地址操作: 1、点击VCS; 2、点击Git; 3、点击Remotes; 具体步骤如图 4、点击框中链接即可在右边看到一个...

west_coast
24分钟前
6
0
将规则集传递给mixin

允许包装在mixin中定义的css块。 分离的规则集是一组CSS属性、嵌套规则集、媒体声明或者是存储在变量中的任何其他内容,我们可以将它包含在规则集中或其他结构中,并且所有属性都将复制到那里...

凌兮洛
25分钟前
4
0
玩转阿里云 Terraform(一):Terraform 是什么

从本文起,我将陆续推出一系列有关 Terraform 的文章,从概念,特点,工作机制,用法以及最佳实践等多个方面由浅入深的向大家介绍如何在阿里云上玩转 Terraform。同时也希望借此机会,与感兴...

阿里云官方博客
26分钟前
4
0
科研大数据面临的挑战

近几十年硬件的发展非常迅猛,第一台Macintosh苹果电脑的内存是128KB(0.13MB),现在很多笔记本配的是8GB的内存,硬盘1TB(1024GB),2TB的很常见。大型的数据服务器上还会有更大的储容量,...

英论阁学术院
26分钟前
5
0
python学习10.09:Python列表和元组的底层实现

有关列表(list)和元组(tuple)的底层实现,本节分别从它们的源码来进行分析。 首先来分析 list 列表,它的具体结构如下所示: typedef struct { PyObject_VAR_HEAD /* Vector o...

太空堡垒185
27分钟前
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部