2012-05-02 61 views
5

RSpec和Factory Girl的新手,並放棄這場戰鬥!測試has_many通過驗證使用RSpc 2.9.0 + FactoryGirl 3.2

我有一個連接表MealItems其中的一個屬性驗證。在軌控制檯我可以成功做到以下幾點:

meal = Meal.create!(...) 
food = Food.create!(...) 
item1 = MealItem.create!(meal, food, 1234) # 1234 being the property that is required 

然後我就可以自動地通過MealItem獲得食物的陣列在一個給定餐是這樣的:

meal.foods 

的問題是,我不能瞭解如何正確地創建工廠,以便在規範中提供這種關係。我可以指定項目一頓飯,並測試這些,而是​​通過關係工作(meal.foods)不能得到的has_many

模式

class Meal < ActiveRecord::Base 

    has_many :meal_items 
    has_many :foods, :through => :meal_items 

end 

class MealItem < ActiveRecord::Base 

    belongs_to :meal 
    belongs_to :food 

    validates_numericality_of :serving_size, :presence => true, 
              :greater_than => 0 
end 

class Food < ActiveRecord::Base 

    has_many :meal_items 
    has_many :meals, :through => :meal_items 

end 

規格/ factories.rb

FactoryGirl.define do 

    factory :lunch, class: Meal do 
    name  "Lunch" 
    eaten_at Time.now 
    end 

    factory :chicken, class: Food do 
    name   "Western Family Bonless Chicken Breast" 
    serving_size 100 
    calories  100 
    fat    2.5 
    carbohydrates 0 
    protein   19 
    end 

    factory :cheese, class: Food do 
    name   "Armstrong Light Cheddar" 
    serving_size 30 
    calories  90 
    fat    6 
    carbohydrates 0 
    protein   8 
    end 

factory :bread, class: Food do 
    name   "'The Big 16' Multigrain Bread" 
    serving_size 38 
    calories  100 
    fat    1 
    carbohydrates 17 
    protein   6 
    end 

    factory :item1, class: MealItem do 
    serving_size  100 
    association  :meal, factory: :lunch 
    association  :food, factory: :chicken 
    end 

    factory :item2, class: MealItem do 
     serving_size  15 
     association  :meal, factory: :lunch 
     association  :food, factory: :cheese 
    end 

    factory :item3, class: MealItem do 
    serving_size  76 
    association  :food, factory: :bread 
    association  :meal, factory: :lunch 
    end  


    factory :meal_with_foods, :parent => :lunch do |lunch| 
    lunch.meal_items { |food| [ food.association(:item1), 
          food.association(:item2), 
          food.association(:item3) 
          ]} 


    end 
end 

規格/型號/ meal_spec.rb

... 

describe "Nutritional Information" do 

before(:each) do 

    #@lunch = FactoryGirl.create(:meal_with_foods) 

    @item1 = FactoryGirl.create(:item1) 
    @item2 = FactoryGirl.create(:item2) 
    @item3 = FactoryGirl.create(:item3) 
    @meal = FactoryGirl.create(:lunch) 

    @meal.meal_items << @item1 
    @meal.meal_items << @item2 
    @meal.meal_items << @item3 

    @total_cals = BigDecimal('345') 
    @total_fat = BigDecimal('7.5') 
    @total_carbs = BigDecimal('34') 
    @total_protein = BigDecimal('35') 

end 

# Would really like to have 
#it "should have the right foods through meal_items" do 
    #@meal.foods[0].should == @item1.food 
#end 

it "should have the right foods through meal_items" do 
    @meal.meal_items[0].food.should == @item1.food 
end 


it "should have the right amount of calories" do 
    @meal.calories.should == @total_cals 
end 

... 

我的問題是:

我將如何設置這些工廠,所以我可以引用Meal.foods在我的測試,我不能直接分配食物,吃飯的,因爲在連接表驗證要求。我在測試過程中沒有正確地將MealItem工廠寫入數據庫,這就是爲什麼我的規範中不存在has_many through關係的原因?

任何幫助,非常感謝。

回答

0

是的,您需要MealItem的工廠,並且您需要在規範中使用該工廠才能通過驗證。如果您除了兩個外鍵之外沒有任何必需的字段,它可能已經工作了。 所以設立工廠默認servering大小之後,將取代這個:

@meal.meal_items << @item1 
@meal.meal_items << @item2 
@meal.meal_items << @item3 

與此:

FactoryGirl.create(:meal_item, :meal => @meal, :item => @item1) 
FactoryGirl.create(:meal_item, :meal => @meal, :item => @item1) 
FactoryGirl.create(:meal_item, :meal => @meal, :item => @item1) 

另一種方法是建立一個默認的模型中的份量。這樣可以讓你保留原有的測試,並且可以更容易在實際代碼中使用你的首選語法。