gpt4 book ai didi

ruby-on-rails - 使用 RSpc 2.9.0 + FactoryGirl 3.2 通过验证测试 has_many

转载 作者:行者123 更新时间:2023-12-03 17:56:47 24 4
gpt4 key购买 nike

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 Factories 写入数据库,这就是为什么我的规范中不存在 has_many through 关系的原因?

任何帮助深表感谢。

最佳答案

是的,您需要 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)

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

关于ruby-on-rails - 使用 RSpc 2.9.0 + FactoryGirl 3.2 通过验证测试 has_many,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10422621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com