5

RSpec 和 Factory Girl 的新手,并且输掉了这场战斗!

我有一个连接表 MealItems,它对它的一个属性进行了验证。在 Rails 控制台中,我可以成功执行以下操作:

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

然后我可以通过 MealItem 自动获取给定膳食中的一系列食物,如下所示:

meal.foods

问题是我无法弄清楚如何正确创建工厂,因此这种关系在规范中可用。我可以将这些物品分配给一顿饭,并对其进行测试,但无法通过关系工作获得 has_many (meal.foods)

楷模

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

规格/工厂.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 关系的原因?

任何帮助深表感谢。

4

1 回答 1

0

是的,您需要 MealItem 的工厂,并且您需要在规范中使用该工厂才能通过验证。如果除了两个外键之外没有任何必填字段,它可能已经可以工作了。因此,在使用默认服务器大小设置工厂后,您将替换它:

@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)

另一种方法是在您的模型中设置默认份量。这将允许您保持测试原样,并且可以更轻松地在实际代码中使用您喜欢的语法。

于 2012-09-07T20:30:53.310 回答