2015-11-21 24 views
0

我試圖測試簡單的POST控制器#創建方法。Attributes_for Ruby on Rails 4測試工廠女孩

it "saves the new object in the DB" do 
    object = build(:object_with_association) 
    expect { post :create, object: object.attributes }.to change(SomeObject, :count).by(1) 
end 

這是object.attributes是做到這一點的最好方法是什麼?我試着使用attributes_for(:object_with_association),但它返回一個沒有關聯的散列。可能會有一些有用的方法來達到這個目的嗎? 我廠:

factory :obj do 
    name "A" 
    association :first, factory: :first 
    association :second, factory: :second 

    factory :obj_with_association do 
    transient do 
     nested_count 5 
    end 

    after(:build) do |obj, evaluator| 
     create_list(:nested, evaluator.nested_count, obj: obj) 
    end 
    end 
end 

協會:第一和:第二個是belongs_to的,爲:嵌套的的has_many

回答