2013年8月29日木曜日

FactoryGirl Traits

FactoryGirlしりーず

Traits

これもよく使う

factory :user do
  name 'murajun1978'
  
  trait :admin do 
    admin true
  end

  trait :general do 
    admin false
  end

  factory :admin_user,   traits: [:admin]
  factory :general_user, traits: [:general]
end

とか

factory :user do 
  name 'murajun1978'
  
  trait :admin do 
    admin true
  end

  trait :general do 
    admin false
  end
end

factory :admin_user, parent: :user do 
  admin
end

こんな感じでもかける

使い方
FactoryGirl.create(:admin_user)
FactoryGirl.create(:user, :general)

どちらでもおk

複数レコードを作りたい場合
FactoryGirl.create(:user, 2, :general)

シーケンスと一緒に使おう

ネタが少なくなってきたw

( ̄(エ) ̄)彡☆

2013年8月27日火曜日

FactoryGirl Inheritance

FactoryGirlしりーず

Inheritance

みんな大好きお手軽継承です

spec/factories/user.rb
FactoryGirl.define do
  factory :user do
    name 'murajun1978'
    password 'murajun1978'
    password_confirmation { |u| u.password }

    factory :admin_user do 
      admin true
    end
  end
end

ブロックの外に書きたい場合は

FactoryGirl.define do
  factory :user do
    name 'murajun1978'
    password 'murajun1978'
    password_confirmation { |u| u.password }
  end

  factory :admin_user, parent: :user do ← ここ
    admin true
  end
end

両方ともこんな感じでつかえるよ

FactoryGirl.create(:user)
FactoryGirl.create(:admin_user)

admin_userでcreateするとadminフラグがtrueのユーザができる

かんたんヘ(^o^)ノ

2013年8月26日月曜日

FactoryGirl Associations

FactoryGirlしりーず

Associations

factories/users.rb
FactoryGirl.define do
  factory :user, aliases: [:author] do
    name 'murajun1978'
    password 'murajun1978'
    password_confirmation { |u| u.password }

    factory :user_posts do
      ignore do ←これ
        count 1
      end

      after(:create) do |user, value|
        FactoryGirl.create_list(:post, value.count, author: user)
      end
    end
  end

  factory :post do 
    author ←ここ
    sequence(:title) {|n| "text#{n}"}
  end
end

factories/post.rb
factory :post do 
  association :author, factory: :user ←ここ
  sequence(:title) {|n| "text#{n}"}
end

でも、このままだとbuildしたときにuserオブジェクトが作られない

factories/post.rb
factory :post do 
  association :author, factory: :user, strategy: :build ←ここ
  sequence(:title) {|n| "text#{n}"}
end

strategy: :buildオプションを指定するとbuildだけでuserオブジェクトも一緒に作成されるよ

明日は継承について書こうかなヘ(^o^)ノ

FactoryGirlしりーずとか書いてるけどFactoryGirlのGetting Startedにすべてがあるので…

そっちを見るのがはやいかも(;・∀・)

2013年8月23日金曜日

FactoryGirl Sequences ignore …

FactoryGirlしりーず

Sequences

これはよく使う
factory :post do 
  author
  sequence(:title) {|n| "test#{n}"} ←これ
end

test1,test2,test3と連番でvalueをセットできるよ

ignore

これは僕は知らなかった
factories/users.rb
FactoryGirl.define do
  factory :user, aliases: [:author] do
    name 'murajun1978'
    password 'murajun1978'
    password_confirmation { |u| u.password }

    factory :user_posts do
      ignore do ←これ
        count 1
      end

      after(:create) do |user, value|
        FactoryGirl.create_list(:post, value.count, author: user)
      end
    end
  end

  factory :post do 
    author
    sequence(:title) {|n| "text#{n}"}
  end
end

models/user.rb
require 'spec_helper'

describe User do
  context "example1" do 
    subject {FactoryGirl.create(:user)}
    its(:name){should eq 'murajun1978'}
  end

  context "example2" do 
    subject {FactoryGirl.create(:post)}
    its("author.name"){should eq "murajun1978"}
  end

  context "example3" do ←これ
    before {FactoryGirl.create(:user_posts, count: 5)}
    it {Post.should have(5).posts}
    it {User.should have(1).user}
  end
end

countを引数で指定するとそのレコード数だけ作成してくれる

countを指定しなければデフォルト値がセットされるよ

上の例だと1回だね

ちなみに…

have(5).postsのpostsは任意なのでitemsとかなんでもおkヘ(^o^)ノ

FactoryGirl Aliases

FactoryGirlとRSpecをすこしずつまとめようとおもう

まずはFactoryGirlのAliasesから

こんなテーブルがあるとする
User
 name
 password_digest

Post
 title
 author_id

model/user.rb
class User < ActiveRecord::Base
  has_secure_password

  has_many :posts
end

model/post.rb
class Post < ActiveRecord::Base
  belongs_to :author, class_name: 'User'
end

factories/users.rb ←ここ
FactoryGirl.define do
  factory :user, aliases: [:author] do
    name 'murajun1978'
    password 'murajun1978'
    password_confirmation { |u| u.password }
  end

  factory :post do 
    author
    title 'test'
  end
end

spec/models/user_spec.rb
require 'spec_helper'

describe User do
  context "example1" do 
    subject {FactoryGirl.create(:user)}
    its(:name){should eq 'murajun1978'}
  end

  context "example2" do 
    subject {FactoryGirl.create(:post)}
    its("author.name"){should eq "murajun1978"}
  end
end

specの書き方はさておき(;・∀・)

こんな書き方ができまするヘ(^o^)ノ

2013年7月12日金曜日

RailsでMySQLに接続できないヘ(^o^)ノ

Railsでmysql2のgemがインストールできないの続き

gemがインストールできたのでscaffoldしてみたー

Library not loaded: libmysqlclient.18.dylib (LoadError)

また、なんか無いって怒られた(;・∀・)

$ sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

再びscaffoldヘ(^o^)ノ

でけた(Rails4の場合)

これでも動かない場合は…

Connector/C 6.1がいるかもしれない

./bundle/config
UNDLE_BUILD__MYSQL: --with-mysql-config=/usr/local/Connector6.1のぱす/bin/mysql_config

もし、gemのmysqlバージョンとインストールされてるバージョンが違うって言われたら…

MySQL Community Server 5.7.1 m11をインストールすると幸せになるかも

実は…Rails3.2.11の環境ではここまでしないと動かなかった(;・∀・)

Postgres.appみたいなのあれば幸せになれるのになー

( ̄(エ) ̄)彡☆

2013年7月11日木曜日

Railsでmysql2のgemがインストールできないヘ(^o^)ノ

RailsとMySQLで開発してると、mysql2のgemインストールすると…

checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
 --with-opt-dir
 --without-opt-dir
 --with-opt-include
 --without-opt-include=${opt-dir}/include
 --with-opt-lib
 --without-opt-lib=${opt-dir}/lib
 --with-make-prog
 --without-make-prog
 --srcdir=.
 --curdir
 --ruby=/Users/murajun1978/.rbenv/versions/2.0.0-p247/bin/ruby
 --with-mysql-config
 --without-mysql-config
 --with-mysql-dir
 --without-mysql-dir
 --with-mysql-include
 --without-mysql-include=${mysql-dir}/include
 --with-mysql-lib
 --without-mysql-lib=${mysql-dir}/
 --with-mysqlclientlib
 --without-mysqlclientlib
 --with-mlib
 --without-mlib
 --with-mysqlclientlib
 --without-mysqlclientlib
 --with-zlib
 --without-zlib
 --with-mysqlclientlib
 --without-mysqlclientlib
 --with-socketlib
 --without-socketlib
 --with-mysqlclientlib
 --without-mysqlclientlib
 --with-nsllib
 --without-nsllib
 --with-mysqlclientlib
 --without-mysqlclientlib
 --with-mygcclib
 --without-mygcclib
 --with-mysqlclientlib
 --without-mysqlclientlib


Gem files will remain installed in /Users/user_name/dev/rails/asa_cloud_system/vendor/bundler/ruby/2.0.0/gems/mysql-2.9.1 for inspection.
Results logged to /Users/user_name/dev/rails/asa_cloud_system/vendor/bundler/ruby/2.0.0/gems/mysql-2.9.1/ext/mysql_api/gem_make.out

An error occurred while installing mysql (2.9.1), and Bundler cannot continue.
Make sure that `gem install mysql -v '2.9.1'` succeeds before bundling.

(゚д゚)!

ならば!
$ bundle config build.mysql --with-mysql-config=/usr/local/mysql/bin/mysql_config

あかん!

ん?ちょっとまてよ…

~/.bundle/config
BUNDLE_BUILD__MYSQL: --with-mysql-config=/usr/local/mysql/bin/mysql_config

BUNDLE_BUILD__MYSQL2: --with-mysql-config=/usr/local/mysql/bin/mysql_config

いけたやん(;・∀・)

そうだよね、mysql2だもんね…

( ̄(エ) ̄)彡☆