Casecommons / with_model

Dynamically build an Active Record model (with table) within a test context
http://www.casebook.net
MIT License
166 stars 18 forks source link

duplicate model names in sibling contexts pollute each other when one is referenced before defined #4

Closed pivotal-casebook closed 13 years ago

pivotal-casebook commented 13 years ago

The problem here is that posts is referenced in the second context before it is defined so the Post class that is referenced is the one from the previous context. You can verify this by looking at the object ids.

  context "when the model has a circular nested attribute reference" do
    with_model :blog do
      table {}
      model do
        has_many :posts
        accepts_nested_attributes_for :posts
      end
    end

    with_model :post do
      table do |t|
        t.integer :blog_id
      end

      model do
        puts self.object_id
        belongs_to :blog
        accepts_nested_attributes_for :blog
      end
    end

    subject { Post.to_xsd }

    it "should generate a valid XSD" do
      validate_xsd(subject)
    end

  end

  context "when the model has a nested reference that references another nested reference" do
    with_model :blog do
      table {}
      model do
        has_many :posts
        has_many :readers
        accepts_nested_attributes_for :posts
        accepts_nested_attributes_for :readers
      end
    end

    with_model :post do
      table do |t|
        t.integer :blog_id
      end

      model do
        puts self.object_id
        belongs_to :blog
        has_many :readers
        accepts_nested_attributes_for :blog
        accepts_nested_attributes_for :readers
      end
    end

    with_model :reader do
      table do |t|
        t.integer :blog_id
        t.integer :post_id
      end
    end

    subject { Post.to_xsd }

    it "should generate a valid XSD" do
      validate_xsd(subject)
    end

  end
end
nertzy commented 13 years ago

This might be because of this:

https://github.com/Casecommons/with_model/issues/1

I have been thinking of standardizing on using constants to refer to the models and losing the method and instance variable.

nertzy commented 13 years ago

I believe this is fixed with b74e6bd8c72cf3f4b631a9341cca6d6e6b7c8f98

If you encounter this bug again, please write a failing spec. Thanks!