factorygirl入門

36
FactoryGirl入門 hamamatsu.rb#9 2011/11/09 @suchi

Upload: shigeru-uchiyama

Post on 12-Nov-2014

18.279 views

Category:

Technology


1 download

DESCRIPTION

hamamatsu.rb #9 FactoryGirl入門(私が)の資料。 https://github.com/hamamatsu-rb/hamamatsu-rb.github.com/wiki 追加(2013/9/1): 最近は trait なんてのがあるようです。こちらがわかりやすかったです→ http://techracho.bpsinc.jp/morimorihoge/2013_08_23/12744

TRANSCRIPT

Page 1: FactoryGirl入門

FactoryGirl入門

hamamatsu.rb#9 2011/11/09

@suchi

Page 2: FactoryGirl入門

なぜ Girlなの?

Page 3: FactoryGirl入門

世界三大 ファクトリー

Page 4: FactoryGirl入門
Page 5: FactoryGirl入門
Page 6: FactoryGirl入門
Page 7: FactoryGirl入門

世界三大ファクトリー

• ファクトリーレコード

–マンチェスターのインディーズレーベル

• アンディ・ウォーホルのファクトリー

–ウォーホルのスタジオ

–当時の先鋭アーティストのたまり場

• うなぎパイファクトリー

–春華堂によるうなぎパイ工場

Page 8: FactoryGirl入門

おそらく

Page 9: FactoryGirl入門

イーディ Factory

Girl

Page 10: FactoryGirl入門
Page 11: FactoryGirl入門

サンプル(BBS)

https://github.com/suchi/fgbbs

Page 12: FactoryGirl入門

Article

class Article < ActiveRecord::Base has_many :attaches has_many :comments validates_presence_of :title validates_presence_of :body validates_presence_of :user validates_uniqueness_of :title end

Page 13: FactoryGirl入門

Attach

class Attach < ActiveRecord::Base belongs_to :article validates_presence_of :filename end

Page 14: FactoryGirl入門

Comment

class Comment < ActiveRecord::Base belongs_to :article, :dependent => :destroy validates_presence_of :body validates_presence_of :user validates_presence_of :article_id end

Page 15: FactoryGirl入門

Fixture(article.yaml)

one: title: タイトル body: こんにちは user: uchiyama two: title: また別のタイトル body: どうもどうも user: Cherencov

Page 16: FactoryGirl入門

FactoryGirl

Factory.define :article |do| f f.title "タイトル" f.user "uchiyama" f.body "こんにちは" end

Page 17: FactoryGirl入門

FactoryGirl

• Factory.create(:article)

– Articleインスタンスを返す

• Factory.create(:article, :title => "別のタイトル", :user => "mackato")

–属性をオーバーライドできる

• a = Factory.build(:article)

• a.changed? #=> true

– buildはDBに保存しない

Page 18: FactoryGirl入門

エラー

>> Factory.create(:article) ActiveRecord::RecordInvalid: Validation failed: Title has already been taken from d:/usr/ruby-1.8/lib/...

Page 19: FactoryGirl入門

Article

class Article < ActiveRecord::Base has_many :attaches has_many :comments validates_presence_of :title validates_presence_of :body validates_presence_of :user validates_uniqueness_of :title end

Page 20: FactoryGirl入門

Sequenceを使う

Factory.sequence(:article) do |n| "article_#{n}" end Factory.next(:article) #=> "article_1" Factory.next(:article) #=> "article_2"

Page 21: FactoryGirl入門

Sequence

[:article_title, :user_name, :body].each do |sym| Factory.sequence(sym) do |n| "#{sym.to_s}_#{n}" end end Factory.next(:article_title) #=> "article_1" Factory.next(:article_title) #=> "article_2" Factory.next(:article_title) #=> "article_3"

Page 22: FactoryGirl入門

Article.titleにシーケンスを使う

Factory.define :article |do| f f.title Factory.next(:article_title) f.user "uchiyama" f.body "こんにちは" end

Page 23: FactoryGirl入門

エラー

Factory.define :article |do| f f.title Factory.next(:article_title) f.user "uchiyama" f.body "こんにちは" end Factory.create(:article) #=> ActiveRecord::RecordInvalid: Validation failed: Title has already been taken

Page 24: FactoryGirl入門

遅延評価

Factory.define :article |do| f f.title { Factory.next(:article_title)} f.user "uchiyama" f.body "こんにちは" end

Page 25: FactoryGirl入門

関連

Page 26: FactoryGirl入門

• シンボルと作成するクラスが異なる場合は:classオプションで指定

Factory.define :article_with_attaches, :class => Article do |f| f.title { Factory.next(:article_title) } f.user 'uchiyama' f.body '"こんにちは' f.attaches [ Factory(:attach), Factory(:attach) ] end

添付ファイル付Article

Page 27: FactoryGirl入門

• →ロード時にエラー

Factory.define :article_with_comment, :class => Article do |f| f.title { Factory.next(:article_title) } f.user 'uchiyama' f.body '"こんにちは' f.comments [ Factory(:comment), Factory(:comment) ] end

コメント付Article

Page 28: FactoryGirl入門

• →インスタンス作成時にエラー

Factory.define :article_with_comment, :class => Article do |f| f.title { Factory.next(:article_title) } f.user 'uchiyama' f.body '"こんにちは' f.comments {[ Factory(:comment), Factory(:comment) ]} end

遅延評価にしてみる

Page 29: FactoryGirl入門

Comment

class Comment < ActiveRecord::Base belongs_to :article, :dependent => :destroy validates_presence_of :body validates_presence_of :user validates_presence_of :article_id end

Page 30: FactoryGirl入門

after_createを使う

Factory.define :article_with_comments, :class => Article do |f| f.title { Factory.next(:article_title) } f.user { Factory.next(:user_name) } f.body 'こんにちは' f.after_create do |article| comments = [ Factory(:comment, :article_id => article.id), Factory(:comment, :article_id => article.id) ] end end

• after_create - Article.createが呼ばれたあと、インスタンスを引数に呼ばれる

Page 31: FactoryGirl入門

その他便利な機能

• Factory.attributes_for(:article)

–属性のhashを返す

– [:title=>"article_title_6", :user=>"uchiyama", :body=>"こんにちは"}

• Factory.stub(:article)

– stubを作成する

Page 32: FactoryGirl入門

欠点 欠点

• Factory名称の名前空間がグローバル

• 記法がちょっとダサい

• 後発のFablicationはもっと洗練されているらしい

• Factory名称の名前空間がグローバル

• 記法がちょっとダサい

• 後発のFablicationはもっと洗練されているらしい

Page 33: FactoryGirl入門

• 実験はコンソールで --sandboxオプションをつけて

> ruby script/console --sandbox

TIPS

Page 34: FactoryGirl入門
Page 35: FactoryGirl入門
Page 36: FactoryGirl入門

Q&A Q&A