2012-03-01 44 views
0

OK,在Thoughtbot的工廠女孩​​頁面非常有幫助,但我做錯了什麼。在基本功能的早期迭代我做了兩個紀錄,並迫使每個以下簡稱例如聯想:FactoryGirl,rails,cucumber:與多個記錄進行關聯

Given /^a price for corn has already been entered$/ do 
    corn = Commodity.create!(name: "Corn", wholesale_unit: "ton") 
    Price.create!(commodity: corn, retail_high: "19") 
end 

現在我希望有兩個價格,使我可以在黃瓜試驗中的平均以確保兩者都是被拉。根據納什的建議,我已經很容易地爲上面的工廠創建了工廠。

FactoryGirl.define do 
    factory :commodity do 
    name 'Corn' 
    wholesale_unit 'ton' 
    end 

    factory :price do 
    sequence(:retail_high) { |n| '19#{n}' } 
    commodity 
    end 
end 

在我更新step.rb文件,我試圖創建一個在第一次迭代的工作,但有兩個記錄的條件:

Given /^there are prices entered$/ do 
    Factory(:commodity) 
    Factory(:price) do 
    sequence(:retail_high) 
    end 
end 

所以我的真正的問題是,我無法到第一個基地,因爲當我使用pry來查看是否創建了一條記錄時,我得到一個Price = nil。與商品一樣。有很多屬性讓工廠工作真的有幫助。以上更新到納什的例子顯示了正確的第一條記錄,但第二條記錄是第一條記錄的重複。以下是我的模型,相關的控制器和模式。感謝名單掛在山姆

commodity.rb:

class Commodity < ActiveRecord::Base 
    has_many :prices 
end 

price.rb:

class Price < ActiveRecord::Base 
    belongs_to :commodity 
end 

commodities_controller.rb:

class CommoditiesController < ApplicationController 
    def show 
    @commodity = Commodity.find(params[:id]) 
    end 

相關schema.rb:

create_table "commodities", :force => true do |t| 
    t.string "name" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.string "wholesale_unit" 
    t.string "retail_unit" 
    t.string "farm_gate_unit" 
    t.string "delivered_unit" 
    t.string "receipt_unit" 
    end 

    create_table "prices", :force => true do |t| 
    t.date  "date" 
    t.string "price_type" 
    t.string "quality" 
    t.string "farm_gate_unit" 
    t.decimal "farm_gate_high" 
    t.decimal "farm_gate_low" 
    t.string "delivered_unit" 
    t.decimal "delivered_high" 
    t.decimal "delivered_low" 
    t.string "wholesale_unit" 
    t.decimal "wholesale_high" 
    t.decimal "wholesale_low" 
    t.string "retail_unit" 
    t.decimal "retail_high" 
    t.decimal "retail_low" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.integer "commodity_id" 
end 

回答

1

我假設你使用FactoryGirl 2.

# factories.rb 

FactoryGirl.define do 
    factory :commodity do 
    name 'Corn' 
    wholesale_unit 'ton' 
    end 

    factory :price do 
    retail_high { Random.new.rand(100..500) } 
    commodity 
    end 
end 

# steps.rb 

Given /^there are prices entered$/ do 
    FactoryGirl.create_list(:price, 2, commodity: Factory(:commodity)) 
end 

這會給你同樣的玉米商品價格2級的對象。 如果你想製作兩種價格不同的商品,你可能會寫:

Given /^there are prices entered$/ do 
    Factory(:price) # this for corn 
    Factory(:price, commodity: Factory(:commodity, name: 'Weat')) 
end 
+0

THX,這個應該工作,唯一不同的東西在我step.rb從你的加入 - > {} binding.pry .CALL 。我有限的撬技能表明我'入'價格和'第一'。這產生了零。 '@relation'產生'[]'。這些命令在過去有效。在試圖消除環境變量時,我提出了其他功能,step.rb文件,但沒有改變。我的問題是否存在? – sam452 2012-03-05 14:54:33

+0

你爲什麼要用Pry測試FactoryGirl?你的測試沒有任何調試工具通過嗎? – 2012-03-05 15:19:08

+0

他們沒有匹配預期的值,所以我回頭工作,如果黃瓜創造了記錄。在之前的測試中,pry可以看到創建的記錄。測試是點擊「玉米」並查看retail_price。我得到的錯誤是「nil:NilClass(ActionView :: Template :: Error)」的undefined方法'retail_high'「。這表明黃瓜無法看到通過控制器鏈接的記錄。零表明它沒有從已知好的數據庫中獲取屬性。通過瀏覽器瀏覽控制器會產生一個不是由黃瓜生成的手動生成的舊記錄。 – sam452 2012-03-05 16:33:29