Ruby on rails 使用RSpc 2.9.0+;工厂女孩3.2
RSpec和工厂女孩的新成员,输掉这场战斗强> 我有一个联接表MealItems,它对它的一个属性进行了验证。在rails控制台中,我可以成功地执行以下操作:Ruby on rails 使用RSpc 2.9.0+;工厂女孩3.2,ruby-on-rails,rspec,factory-bot,Ruby On Rails,Rspec,Factory Bot,RSpec和工厂女孩的新成员,输掉这场战斗 我有一个联接表MealItems,它对它的一个属性进行了验证。在rails控制台中,我可以成功地执行以下操作: meal = Meal.create!( ... ) food = Food.create!( ... ) item1 = MealItem.create!( meal, food, 1234 ) # 1234 being the property that is required 然后,我可以通过MealItem自动获取给定膳食中的一系
meal = Meal.create!( ... )
food = Food.create!( ... )
item1 = MealItem.create!( meal, food, 1234 ) # 1234 being the property that is required
然后,我可以通过MealItem自动获取给定膳食中的一系列食物,如下所示:
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
...
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
...
。。。
描述“营养信息”吗
在…之前做
#@午餐=工厂女孩。创建(:带食物的膳食)
@item1=FactoryGirl.create(:item1)
@item2=FactoryGirl.create(:item2)
@item3=FactoryGirl.create(:item3)
@用餐=工厂女孩。创建(:午餐)
@是的,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)