2012-09-10 164 views
2

我剛剛開放Factory_Girl,我想弄清楚如何建立依賴工廠,大多數問題似乎由於新版本而過時。工廠女孩協會4.0.0

我使用的是聯想,但不是創建關聯的對象之外,該對象似乎不相關或以任何方式

基本上這裏涉及到的主要對象是我所

factory :computer do 
    serial_no "12345" 
end 

factory :allocation do 
    association :computer_id, factory: :computer 
end 

分配belongs_to計算機和計算機has_many分配 基本上分配是任何時候計算機被移動或什麼的記錄。

我不知道我在做什麼錯,但每次運行這個時,分配的computer_id都是'1',但計算機的ID是隨機的(通常是0-20之間的數字),並且那麼我的測試失敗,因爲它找不到合適的計算機對象。

編輯: 就好像它不夠混亂,實際的班級名稱是Assignment,我試圖簡單。下面是涉及的實際代碼,實際的代碼沒有問題,因爲在創建過程中,computer_id和user_id作爲參數傳遞給create方法。

describe "GET index" do 
    it "assigns assignments as @assignment" do 
    Assignment.any_instance.stubs(:valid?).returns(true) 
    assignment = FactoryGirl.create :associated_assignment 
    get :index, {}, valid_session 
    assigns(:assignments).should eq([assignment]) 
    end 
end 

所涉及的工廠是

factory :user do 
    fname "John" 
    lname "Smith" 
    uname "jsmith" 
    position "placeholder" 
end 

factory :computer do 
    asset_tag "12345" 
    computer_name "comp1" 
    make "dell" 
    model "E6400" 
    serial_no "abc123" 
end 

factory :associated_assignment, class: Assignment do 
    association :user_id, factory: :user 
    association :computer_id, factory: :computer 
    assign_date '11-11-2008' 
end 

而且控制器是:

def index 
    @assignments = [] 
    @computers = Computer.all 
    Computer.all.each do |asset| 
    @assignments << Assignment.where(:computer_id => asset.id).order("assign_date ASC").last 
    end 
    respond_to do |format| 
    format.html # index.html.erb 
    format.json { render json: @assignments } 
    format.xls { send_data @assignments.to_xls } 
end 

目前我運行這種替代測試來檢查我的id:

describe "GET index" do 
    it "assigns assignments as @assignment" do 
    Assignment.any_instance.stubs(:valid?).returns(true) 
    assignment = FactoryGirl.create :associated_assignment 
    get :index, {}, valid_session 
    assigns(:computers).should eq([assignment]) 
    end 
end 

其中返回如此mething到以下內容,其中計算機的ID是隨機的,但分配的COMPUTER_ID的影響始終爲1

Failure/Error: assigns(:computers).should eq([assignment]) 

    expected: [#<Assignment id: 12, user_id: 1, computer_id: 1, assign_date: "2008-11-11", created_at: "2012-09-10 23:59:48", updated_at: "2012-09-10 23:59:48">] 
     got: [#<Computer id: 14, serial_no: "abc123", asset_tag: 12345, computer_name: "comp1", make: "dell", model: "E6400", created_at: "2012-09-10 23:59:48", updated_at: "2012-09-10 23:59:48">] 

回答

2

工廠並不能保證什麼IDS什麼都會有。但您可以通過以下方式找到適當的計算機對象:

allocation = FactoryGirl.create(:allocation) 
computer = allocation.computer 
+1

感謝在清除了,雖然我以什麼關聯的點,現在是,如果所做的只是產生真糊塗與工廠沒有保證關係的工廠稱之爲工廠。 – corodio8

+0

這種關係是有保證的,只是不是id。您可以在工廠創建分配,並免費獲得關聯的計算機。 –

+0

究竟是什麼決定了這種關聯?在我的情況下,我試圖將分配上的computer_id與工廠上的id鏈接(在實際代碼中工作正常),但是computer_id與關聯計算機的id完全無關。 – corodio8

1

我認爲問題出在:computer_id vs.:computer。下面是做到這一點的一種方式是使用FactoryGirl的推斷工廠和協會的能力:

factory :computer do 
    serial_no "12345" 
end 

factory :allocation do 
    computer 
end 

此外,如果你想每臺計算機在您的規格唯一的序列號,使用:

sequence :serial_no do |n| 
    "1234#{n}" 
end 

factory :computer do 
    serial_no 
end 

factory :allocation do 
    computer 
end 

廠女孩知道你的模型及其關聯,所以它可以提取它們並推斷如何創建相關對象。

這樣:

allocation = FactoryGirl.create :allocation 

創建一個配置對象,並與12340.序列號的計算機對象的ID將已經在分配的COMPUTER_ID場這樣的關係完全建立相關的電腦。 allocation.computer將工作,並且allocation.computer_id將與allocation.computer.id相同。

這使用FactoryGirl的一些語法糖。你可以通過提供協會(場)名稱及廠名更明確:

factory :computer do 
    serial_no "12345" 
end 

factory :allocation do 
    association :computer, factory: :computer 
end