FactoryGirl

Yeng Tsan
1 min readApr 6, 2017

--

To find out what does factory girl do in console:

  1. require “factory_girl” => require the gem
  2. require “./factories” => require the setting of the factory girl
  3. include FactoryGirl::Syntax::Methods

After Step 2, we can use FactoryGirl.new, FactoryGirl.create, but we can user some syntax sugar by include ‘FactoryGirl::Syntax::Methods’

  • build(:user)
  • attributes_for(:user)
  • attributes_for_pair(:user) =>used for relationship, it contains object_id
  • build_stubbed(:user)

If you need a uniq data, you can use: sequence

factory :user do
name 'John' => do not be uniq
sequence(:email) {|n| "#{n}@example.com" => must be uniq
end

https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#configure-your-test-suite

--

--

Yeng Tsan
Yeng Tsan

Written by Yeng Tsan

Software developer, Career consultant, Product manager.開啟你的海外職涯,從日本開始 https://engineer.taiwan-career.com/

No responses yet