私は2つの仕様を持っています。どちらも共通の before(:all) ブロックからのデータをテストします。両方を実行すると、2 つ目は失敗します。
いくつかのログ ステートメントを入れると、週 1 が @h_1 に 2 回追加されているように見えますが、これはどこで起こっているのでしょうか?
最初のブロックのどこかにあるようです。最初のものをコメントアウトすると、2 番目のものはパスします。私が理解できないのは、構文のどこで2つ@w_1を@h_1に2回追加すると言っているのですか?
require 'data_mapper'
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}../data/prod.db")
class Hobby
include DataMapper::Resource
property :id, String, :key => true
has n, :weeks, :through => Resource
end
class Week
include DataMapper::Resource
property :id, Integer, :key => true
has n, :hobbys, :through => Resource
end
DataMapper.finalize.auto_migrate!
describe "Hobby" do
before(:all){
@h_1 = Hobby.create(:id => "zombies")
@w_1 = Week.create(:id => 1)
@w_2 = Week.create(:id => 2)
@h_1.weeks << @w_1
@h_1.weeks << @w_2
@h_1.save
}
context "when a week is added to hobby" do
subject { @h_1.weeks[0] }
it "should be stored in the 'weeks' property of the hobby" do
should eql @w_1 #if I comment out this the spec below will pass
end
end
context "when another week is added to hobby" do
it "hobby should have two weeks" do
@h_1.weeks.length.should eql 2
@h_1.weeks.first(:id => 2).should_not eql nil
end
end
end