Ruby on rails 3 FactoryGirl、rails、cucumber:与多个记录关联
好的,thoughtbot的factoryGirl页面非常有用,但我做错了什么。在一个基本功能的早期迭代中,我做了两个记录,并根据下面的简化示例强制关联:Ruby on rails 3 FactoryGirl、rails、cucumber:与多个记录关联,ruby-on-rails-3,cucumber,factory-bot,Ruby On Rails 3,Cucumber,Factory Bot,好的,thoughtbot的factoryGirl页面非常有用,但我做错了什么。在一个基本功能的早期迭代中,我做了两个记录,并根据下面的简化示例强制关联: Given /^a price for corn has already been entered$/ do corn = Commodity.create!(name: "Corn", wholesale_unit: "ton") Price.create!(commodity: corn, retail_high: "19"
Given /^a price for corn has already been entered$/ do
corn = Commodity.create!(name: "Corn", wholesale_unit: "ton")
Price.create!(commodity: corn, retail_high: "19")
end
现在我想做两个价格,这样我就可以测试黄瓜的平均价格,以确保两者都被拉扯。根据纳什的建议,我很容易就为上述项目创建了工厂
FactoryGirl.define do
factory :commodity do
name 'Corn'
wholesale_unit 'ton'
end
factory :price do
sequence(:retail_high) { |n| '19#{n}' }
commodity
end
end
在更新的step.rb文件中,我尝试创建在第一次迭代中起作用的条件,但有两条记录:
Given /^there are prices entered$/ do
Factory(:commodity)
Factory(:price) do
sequence(:retail_high)
end
end
所以我真正的问题是我无法到达一垒,因为当我使用pry查看是否正在创建记录时,我得到的价格=零。商品也是如此。有这么多的属性,让工厂工作真的会有帮助。将上述内容更新为下面的纳什示例显示了正确的第一条记录,但第二条记录是第一条记录的重复。以下是我的模型、相关控制器和模式。谢谢你坚持住,山姆
commodity.rb:
class Commodity < ActiveRecord::Base
has_many :prices
end
我想你用的是FactoryGirl 2
# factories.rb
FactoryGirl.define do
factory :commodity do
name 'Corn'
wholesale_unit 'ton'
end
factory :price do
retail_high { Random.new.rand(100..500) }
commodity
end
end
# steps.rb
Given /^there are prices entered$/ do
FactoryGirl.create_list(:price, 2, commodity: Factory(:commodity))
end
这将为同一种玉米商品提供两个价格对象。
如果你想为不同的商品编造两个价格,你可以写:
Given /^there are prices entered$/ do
Factory(:price) # this for corn
Factory(:price, commodity: Factory(:commodity, name: 'Weat'))
end
Thx,这应该行得通,我的step.rb与你的唯一不同之处是添加->{binding.pry}.call。我有限的打探技巧建议我将“cd”分为“Price”和“first”。结果是零。”@关系“产生”[]。这些命令在过去是有效的。在试图消除环境变量的过程中,我提取了其他功能,step.rb文件,但没有任何更改。是我的绑定问题吗?你为什么要用pry测试FactoryGirl?你的测试通过时没有任何调试工具吗?它们没有匹配预期的值,所以我回过头来研究Cumber是否创建了记录。在以前的测试中,pry可以看到记录是创建的。测试是点击“玉米”并查看零售价格。我得到的错误是“nil:NilClass(ActionView::Template::error)的未定义方法'retail_high'。这表明cucumber无法看到通过控制器链接的记录。Nil表示它没有从已知良好的数据库中获取属性。通过浏览器浏览控制器会生成一个手动生成的旧记录,cucumber没有生成。我将我的模型更改为has_many:prices,并将节目更改为从@commodity.prices.each中提取。我确实看到了这两种价格,但第二种价格又是第一种价格。零售价高+>“19”。我现在更接近了,但为什么两份价格相同?谢谢你让我走到这一步。请用相关型号、控制器和迁移的列表更新你的问题。
create_table "commodities", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.string "wholesale_unit"
t.string "retail_unit"
t.string "farm_gate_unit"
t.string "delivered_unit"
t.string "receipt_unit"
end
create_table "prices", :force => true do |t|
t.date "date"
t.string "price_type"
t.string "quality"
t.string "farm_gate_unit"
t.decimal "farm_gate_high"
t.decimal "farm_gate_low"
t.string "delivered_unit"
t.decimal "delivered_high"
t.decimal "delivered_low"
t.string "wholesale_unit"
t.decimal "wholesale_high"
t.decimal "wholesale_low"
t.string "retail_unit"
t.decimal "retail_high"
t.decimal "retail_low"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "commodity_id"
end
# factories.rb
FactoryGirl.define do
factory :commodity do
name 'Corn'
wholesale_unit 'ton'
end
factory :price do
retail_high { Random.new.rand(100..500) }
commodity
end
end
# steps.rb
Given /^there are prices entered$/ do
FactoryGirl.create_list(:price, 2, commodity: Factory(:commodity))
end
Given /^there are prices entered$/ do
Factory(:price) # this for corn
Factory(:price, commodity: Factory(:commodity, name: 'Weat'))
end