Day11 测试写起乃-FactoryBot-create、build、build_stubbed

建立bot

官方文件有说有以下建立方式至於差别在哪呢?

# Returns a User instance that's not saved
user = build(:user)

# Returns a saved User instance
user = create(:user)

# Returns a hash of attributes that can be used to build a User instance
attrs = attributes_for(:user) # {name: 'CK', phone: '123'}

# Returns an object with all defined attributes stubbed out
stub = build_stubbed(:user)

# Passing a block to any of the methods above will yield the return object
create(:user) do |user|
  user.posts.create(attributes_for(:post))
end

create => 真的写进db

build => 还没save 且 id为 nil

build_stubbed => stub 的概念创建一个假的实体而且还有 id

attributes_for => 拿到 FactoryBot 的 attribute

我平常使用起来,如果你建立的资料能不进就不进!速度会快上很多!

因为有时候建立资料的时候关联的问题可能你只需要测试 profile 但一定要有 user,这时候 user 就可以用 build_stubbed,既能拿到资料也能不会进db commit!

如果还是不懂build、create、build_stubbed的可以看看这篇
FactoryGirl create vs build vs build_stubbed
attributes_for


<<:  【心得】Sublime TexT 3 即时连线

>>:  Day 11 - 基本语法6(回圈2)

D28 Selenium 测试网页与参数

昨天用Selenium IDE 测试网页但是一值找不到方法设随机变数 今天找了python套件sel...

D15 - 转移资料到TiDB工具介绍(二)

接下来改用DM来试试看,首先一样先透过tiup安装DM。 tiup install dm 产生拓墣的...

31 赛後想法与游戏连结

再贴一次游戏规则 建立游戏: 按下 "Start a game" 後,下面有 &...

Day1_前言

哈罗大家好呀~在这里的30天,会一层一层的带给大家制做网页的技巧和方法,我们时常在网页看到的样式及功...

Day 23 Git → Gitlab-CI 超简单

会讲这个题目其实有点微妙,但是都讲完了测试,却不讲 CI 我觉得好像少了一点什麽。 不知道 Andr...