2013-10-27 18 views
0

我嘗試通過這兩個創建控制器規格,但由於某種原因它不驗證Item對象。難道FactoryGirl.attributes_for(:item)缺少profileattachment所需的關聯嗎?如果是這樣,我怎麼能把它傳遞給屬性呢?FactoryGirl.attributes_for(:item)不傳遞POST #create

describe "POST #create" do 

    context "signed in" do 
     login_user 

     context "with valid attributes" do 
     it "creates a new item" do 
      expect{ 
      post :create, trend: FactoryGirl.attributes_for(:item) 
      }.to change(Item,:count).by(1) 
     end 
     it "redirects to the home page" do 
      post :create, item: FactoryGirl.attributes_for(:item) 
      response.should redirect_to Item.last 
     end 
     end 

    end 

我得到了這些錯誤

Failures: 

    1) ItemsController POST #create signed in with valid attributes creates a new item 
    Failure/Error: expect{ 
     count should have been changed by 1, but was changed by 0 
    # ./spec/controllers/items_controller_spec.rb:42:in `block (5 levels) in <top (required)>' 

    2) ItemsController POST #create signed in with valid attributes redirects to the home page 
    Failure/Error: response.should redirect_to Item.last 
     Expected response to be a <redirect>, but was <200> 
    # ./spec/controllers/items_controller_spec.rb:48:in `block (5 levels) in <top (required)>' 

這是Item工廠

FactoryGirl.define do 
    factory :item do 
    profile 

    after(:build) do |item| 
     item.attachments << FactoryGirl.build(:attachment, attachable: item) 
    end 
    end 

end 

回答

1

我猜做FactoryGirl.attributes_for實際上不是一個構建,所以後(:編譯)不會開火。這個怎麼樣:

FactoryGirl.attributes_for(:item, :profile_attributes => FactoryGirl.attributes_for(:profile), :attachments => [FactoryGirl.attributes_for(:subject)]) 

可能是一個好主意,嘗試在軌控制檯此行首先看到的是哈希算法得出什麼出像:)