lxyuma BLOG

開発関係のメモ

factory_girl in rails3その2

factory_girl in rails3その1
http://d.hatena.ne.jp/lxyuma/20121201/1354365069

これの続き。

と言っても、すぐ終わる。


今、2つのfactoriesがある。

FactoryGirl.define do
  factory :user do
    name "MyString"
  end
end

FactoryGirl.define do
  factory :blog do
    title "MyString"
    description "MyString"
    user_id 1
  end
end

user : blog = 1 : n

のデータにしたい!

FactoryGirl.define do
  factory :blog do
    title "MyString"
    description "MyString"
    user #追加
  end
end

userにするだけ。(片方のファイルのfactoryの次の名前ね)

assertしてみる

require 'test_helper'

class BlogTest < ActiveSupport::TestCase
  test "success saving" do
    @blog = FactoryGirl.create(:blog)
    assert_equal "MyString", @blog.user.name #修正
  end
end

勿論、通る。

簡単だなー。

他、シーケンスやら、色々できる。。。

https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md

きょうはこのぐらいで終了。。。