- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
RSpec 和 Factory Girl 的新手,输掉这场战斗!
我有一个连接表 MealItems,它对它的一个属性进行了验证。在 rails 控制台中,我可以成功执行以下操作:
meal = Meal.create!( ... )
food = Food.create!( ... )
item1 = MealItem.create!( meal, food, 1234 ) # 1234 being the property that is required
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
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
...
最佳答案
是的,您需要 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/
是否可以将一个关联用作特征中另一个关联的值? 让我描述一个(简化的)例子。我有 3 个模型 用户 论坛 (belongs_to :forum) 主题 (belongs_to :forum, belon
我正在尝试为我的 Image 对象的一些测试创建一些记录。但是,似乎它们并没有持续存在: describe Image do before(:all) do for i in 0.
我从事的项目结构如下: projects/warehouse/core projects/warehouse/products/bottles projects/warehouse/products/
我有两个相关模型——Customer,它有一个Address。这些是我的工厂: FactoryGirl.define do factory :customer do address e
我有一个使用 rspec 和 factorygirl 的 ruby 应用程序,但我在构建工厂时遇到了问题。当我运行规范时,我得到一个 ArgumentError: missing keywords
好的伙计们。这怎么说得通?两个嵌套的工厂(被 FactoryGirl 认为是继承)不应该相互冲突。到底他妈发生了什么?它要么不是继承,要么是。如果不是,我不知道为什么他们会称之为继承。我只是做错了什么
这可能很简单,但我在任何地方都找不到示例。 我有两个工厂: FactoryGirl.define do factory :profile do user title "direct
我在我的装置中使用 FactoryGirl,但我发现它并没有真正产生有用的验证错误。 我总是收到 activerecord.errors.models.messages.record_invalid
我有一个工厂,我在 factories/locations.rb 中定义了一个位置.我正在使用带有 ruby 1.9.3 的 Mongoid 和 Rails 3.1.1。 FactoryGirl.
我试图弄清楚如何编写属于 2 个不同模型的工厂,每个模型都应该具有相同的父模型。这是人为的示例代码: class User < ActiveRecord::Base has_many :widge
当我在开发中启动 Rails 控制台时,我看到 FactoryGirl 创建对象。显然我做错了,但是这样做的正确方法是什么?这段代码使我的测试工作...... # tests/factories/bo
我正在尝试与工厂女孩建立 has_many:has_many 关系。 这是我的模型: class User < ActiveRecord::Base has_many :user_roles
有没有办法拥有同一个工厂的多个版本?例如,user工厂。 FactoryGirl.define do factory :user#1 do name 'John Doe' date
您好,我正在使用带有 Rails 4.2.11 的 FactoryGirl 4.9.0。我向名为 Query 的模型添加了一个 bool 列,如下所示: class AddSetLatestResul
我在使用 FactoryGirl 时遇到问题,我正在使用序列来避免重复字段,但无论如何验证都失败了。 输出: 1) CustomersController anonymous user GET #
假设我有以下 ActiveRecord楷模: class Car belongs_to :driver end class Driver # Has attribute :name has
FactoryGirl 是否可以定义一个从 0 到 10 的随机数? factory :rating do ranking 1 #random number? re
我有一个标准的has_many关系(预订有很多订单),并确认没有至少一个订单不会保存预订。我正在尝试使用FactoryGirl工厂复制此文件,但是验证阻止我这样做。 class Booking < A
这些是2个简单的模型: class Post :destroy validates :asset, presence: true end class Asset post) end e
我有一个这样的代码模型工厂: Factory.define :code do |f| f.value "code" f.association :code_type f.ass
我是一名优秀的程序员,十分优秀!