Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix memory leak: do not add trait duplications to @defined_traits

Closes #588
  • Loading branch information...
commit 664c426bd0297c8eff31381b682547334ca88f50 1 parent ed2309a
@greyblake greyblake authored joshuaclayton committed
View
4 lib/factory_girl/definition.rb
@@ -6,7 +6,7 @@ class Definition
def initialize(name = nil, base_traits = [])
@declarations = DeclarationList.new(name)
@callbacks = []
- @defined_traits = []
+ @defined_traits = Set.new
@to_create = nil
@base_traits = base_traits
@additional_traits = []
@@ -77,7 +77,7 @@ def skip_create
end
def define_trait(trait)
- @defined_traits << trait
+ @defined_traits.add(trait)
end
def define_constructor(&block)
View
8 spec/factory_girl/definition_spec.rb
@@ -32,7 +32,13 @@
it "maintains a list of traits" do
subject.define_trait(trait_1)
subject.define_trait(trait_2)
- expect(subject.defined_traits).to eq [trait_1, trait_2]
+ expect(subject.defined_traits).to include(trait_1, trait_2)
+ end
+
+ it "adds only unique traits" do
+ subject.define_trait(trait_1)
+ subject.define_trait(trait_1)
+ expect(subject.defined_traits.size).to eq 1
end
end
View
4 spec/support/matchers/trait.rb
@@ -1,6 +1,8 @@
RSpec::Matchers.define :have_trait do |trait_name|
match do |instance|
- instance.defined_traits.include?(FactoryGirl::Trait.new(trait_name, &@block))
+ instance.defined_traits.any? do |trait|
+ trait.name == trait_name && trait.send(:block) == @block
+ end
end
chain :with_block do |block|
Please sign in to comment.
Something went wrong with that request. Please try again.