まずは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^)ノ
0 件のコメント:
コメントを投稿