0
OK,在Thoughtbot的工厂女孩页面非常有帮助,但我做错了什么。在基本功能的早期迭代我做了两个纪录,并迫使每个以下简称例如联想:FactoryGirl,rails,cucumber:与多个记录进行关联
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来查看是否创建了一条记录时,我得到一个Price = nil。与商品一样。有很多属性让工厂工作真的有帮助。以上更新到纳什的例子显示了正确的第一条记录,但第二条记录是第一条记录的重复。以下是我的模型,相关的控制器和模式。感谢名单挂在山姆
commodity.rb:
class Commodity < ActiveRecord::Base
has_many :prices
end
price.rb:
class Price < ActiveRecord::Base
belongs_to :commodity
end
commodities_controller.rb:
class CommoditiesController < ApplicationController
def show
@commodity = Commodity.find(params[:id])
end
相关schema.rb:
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
THX,这个应该工作,唯一不同的东西在我step.rb从你的加入 - > {} binding.pry .CALL 。我有限的撬技能表明我'入'价格和'第一'。这产生了零。 '@relation'产生'[]'。这些命令在过去有效。在试图消除环境变量时,我提出了其他功能,step.rb文件,但没有改变。我的问题是否存在? – sam452 2012-03-05 14:54:33
你为什么要用Pry测试FactoryGirl?你的测试没有任何调试工具通过吗? – 2012-03-05 15:19:08
他们没有匹配预期的值,所以我回头工作,如果黄瓜创造了记录。在之前的测试中,pry可以看到创建的记录。测试是点击“玉米”并查看retail_price。我得到的错误是“nil:NilClass(ActionView :: Template :: Error)”的undefined方法'retail_high'“。这表明黄瓜无法看到通过控制器链接的记录。零表明它没有从已知好的数据库中获取属性。通过浏览器浏览控制器会产生一个不是由黄瓜生成的手动生成的旧记录。 – sam452 2012-03-05 16:33:29