2
我使用FactoryGirl爲Rails相關的gem創建日期維度模型的實例。我廠是這樣的:FactoryGirl序列不遞增
FactoryGirl.define do
sequence :next_day do |n|
Date.new(2000,12,31) + n.days
end
factory :date_dimension do
the_date = FactoryGirl.generate(:next_day)
date {the_date.to_s}
calendar_year {the_date.strftime("%Y")}
(...other attributes created similarly to calendar_year)
end
end
出於無奈我實際上建立了一個小測試,以證明有什麼不工作:
describe "working date factories" do
before(:all) do
@date_dimension = FactoryGirl.create(:date_dimension)
@jan_two = FactoryGirl.create(:date_dimension)
end
describe "sequence incrementing" do
it "returns a date dimension object ok" do
@date_dimension.date.should == "2001-01-01"
end
it "returns the next date in the sequence" do
@jan_two.date.should == "2001-01-02"
end
end
end
當我運行測試,我得到:
working date factories
sequence incrementing
returns a date dimension object ok
returns the next date in the sequence (FAILED - 1)
Failures:
1) working date factories sequence incrementing returns the next date in the sequence
Failure/Error: @jan_two.date.should == "2001-01-02"
expected: "2001-01-02"
got: "2001-01-01" (using ==)
我讀過一系列與序列有關的其他問題,但似乎並沒有發現其中的錯誤。這是一個不同的(可能是數字)錯誤。它是什麼?
我真的無法重現你的問題。我試着定義一個幾乎完全相同的工廠,並且它的順序很好。然後我懷疑它可能與使用塊和date_dimension工廠內的局部變量有關,但無濟於事。對我來說,它工作得很好.. – Tigraine
嗯,計數器是從0開始的嗎? – apneadiving
我的理論是,如果第一個例子通過,那表明計數器從1開始,因爲12/31/2000 + 1.day應該是1/1/2001。它通過,所以我認爲這表明計數器從1開始。 –