0

我有两个规格。它们都测试来自公共 before(:all) 块的数据。如果我同时运行,第二个失败。

放入一些日志语句,似乎第 1 周被添加到 @h_1 两次 - 但这是在哪里发生的?

它似乎在第一个 it 块中的某个地方。如果我注释掉第一个,第二个就会通过。我不明白的是,它在语法中的哪里说两个将@w_1 添加到@h_1 两次?

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
4

0 回答 0