0

I have two specs. They both test data from a common before(:all) block. If I run both, the second one fails.

Putting in some logging statements, it seems like Week 1 is being added to @h_1 twice - but where is this happening?

It seems to be somewhere in the first it block. If I comment out the first, the second one passes. What I don't understand is, where in the syntax is it saying two add @w_1 to @h_1 twice?

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
JoeyC
  • 764
  • 11
  • 19
  • Do you need to remove the records you create, in an after(:all) block? Failing to do this properly has sometimes tripped me up, and produced unexpected results. – DavB Apr 05 '13 at 14:08
  • the thing is, I am checking that the second record gets added and, thus, a many-to-many relationship. If I were to remove the \@w_1 record I would have to add it back again when running the second spec. It seems like a non DRY way of doing things. IMO. Have I understood your comment correctly, @DavB ? – JoeyC Apr 08 '13 at 03:58
  • What I'm getting at is that once your tests have run, won't the records you created in the before(:all) block still be in the database? If you run the tests a second time, I don't think your tests will be behaving as you expect. For example, h_1 = Hobby.create(:id => "zombies") will not return a hobby object because this Hobby will already exist in the database. I'm thinking h_1 will equal false (I can't actually check this right now). – DavB Apr 08 '13 at 20:02
  • That would be the case if the finalize statement was an "upgrade!" However, it is a "migrate!" which will wipe out all the data before running the tests. I have a different implementation when I include it into the main test suite. But for now, this seems to be the simplest example for demonstration – JoeyC Apr 10 '13 at 07:53

0 Answers0