使用RSpc 2.9.0 + FactoryGirl 3.2进行验证测试has_many

时间:2012-05-02 22:30:46

标签: ruby-on-rails rspec factory-bot

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

规格/ 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关系?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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)

另一种方法是在模型中设置默认的服务大小。这样您就可以按原样保留测试,并且可以更容易在实际代码中使用首选语法。