From 3bef85893006f18f23ef27216e1384c7d2912e24 Mon Sep 17 00:00:00 2001 From: Joshua Clayton Date: Fri, 7 Oct 2011 16:43:36 -0400 Subject: [PATCH] Make Factory#attributes protected --- lib/factory_girl/factory.rb | 24 ++++----- .../attribute_existing_on_object.rb | 21 ++++++++ spec/factory_girl/definition_proxy_spec.rb | 19 ------- spec/factory_girl/factory_spec.rb | 50 +------------------ 4 files changed, 34 insertions(+), 80 deletions(-) create mode 100644 spec/acceptance/attribute_existing_on_object.rb diff --git a/lib/factory_girl/factory.rb b/lib/factory_girl/factory.rb index 019f09037..a97bd2d2b 100644 --- a/lib/factory_girl/factory.rb +++ b/lib/factory_girl/factory.rb @@ -50,18 +50,6 @@ def add_callback(name, &block) @attribute_list.add_callback(Callback.new(name, block)) end - def attributes - ensure_compiled - AttributeList.new.tap do |list| - @traits.reverse.map { |name| trait_by_name(name) }.each do |trait| - list.apply_attributes(trait.attributes) - end - - list.apply_attributes(@attribute_list) - list.apply_attributes(parent.attributes) if parent - end - end - def run(proxy_class, overrides) #:nodoc: ensure_compiled proxy = proxy_class.new(build_class) @@ -149,6 +137,18 @@ def add_child(factory) @children << factory unless @children.include?(factory) end + def attributes + ensure_compiled + AttributeList.new.tap do |list| + @traits.reverse.map { |name| trait_by_name(name) }.each do |trait| + list.apply_attributes(trait.attributes) + end + + list.apply_attributes(@attribute_list) + list.apply_attributes(parent.attributes) if parent + end + end + private def callbacks diff --git a/spec/acceptance/attribute_existing_on_object.rb b/spec/acceptance/attribute_existing_on_object.rb new file mode 100644 index 000000000..4a776f0f9 --- /dev/null +++ b/spec/acceptance/attribute_existing_on_object.rb @@ -0,0 +1,21 @@ +require "spec_helper" + +describe "declaring attributes on a Factory that are private methods on Object" do + before do + define_model("Website", :system => :boolean, :link => :string, :sleep => :integer) + + FactoryGirl.define do + factory :website do + system false + link "http://example.com" + sleep 15 + end + end + end + + subject { FactoryGirl.build(:website, :sleep => -5) } + + its(:system) { should == false } + its(:link) { should == "http://example.com" } + its(:sleep) { should == -5 } +end diff --git a/spec/factory_girl/definition_proxy_spec.rb b/spec/factory_girl/definition_proxy_spec.rb index 54a2b1513..891834638 100644 --- a/spec/factory_girl/definition_proxy_spec.rb +++ b/spec/factory_girl/definition_proxy_spec.rb @@ -4,20 +4,6 @@ let(:factory) { FactoryGirl::Factory.new(:object) } subject { FactoryGirl::DefinitionProxy.new(factory) } - def attributes - factory.attributes - end - - it "should add a static attribute for type" do - subject.type 'value' - attributes.to_a.last.should be_kind_of(FactoryGirl::Attribute::Static) - end - - it "should add a static attribute for id" do - subject.id 'value' - attributes.to_a.last.should be_kind_of(FactoryGirl::Attribute::Static) - end - it "should add a static attribute when an attribute is defined with a value" do attribute = stub('attribute', :name => :name) FactoryGirl::Attribute::Static.stubs(:new => attribute) @@ -45,11 +31,6 @@ def attributes }.should raise_error(FactoryGirl::AttributeDefinitionError) end - it "should add an attribute with a built-in private method" do - subject.instance_eval { sleep(0.1) } - attributes.map { |attribute| attribute.name }.should == [:sleep] - end - describe "child factories" do its(:child_factories) { should == [] } diff --git a/spec/factory_girl/factory_spec.rb b/spec/factory_girl/factory_spec.rb index 093da5a9a..5b2614415 100644 --- a/spec/factory_girl/factory_spec.rb +++ b/spec/factory_girl/factory_spec.rb @@ -52,7 +52,7 @@ factory = FactoryGirl::Factory.new(:post) lambda { factory.declare_attribute(FactoryGirl::Declaration::Association.new(:parent, { :factory => :post })) - factory.attributes + factory.ensure_compiled }.should raise_error(FactoryGirl::AssociationDefinitionError) end @@ -117,54 +117,6 @@ child.ensure_compiled child.build_class.should == String end - - describe "given a parent with attributes" do - before do - @parent_attr = :name - @factory.declare_attribute(FactoryGirl::Declaration::Static.new(@parent_attr, 'value')) - end - - it "should create a new factory with attributes of the parent" do - child = FactoryGirl::Factory.new(:child, :parent => @factory.name) - child.ensure_compiled - child.attributes.size.should == 1 - child.attributes.first.name.should == @parent_attr - end - - it "should allow a child to define additional attributes" do - child = FactoryGirl::Factory.new(:child, :parent => @factory.name) - child.declare_attribute(FactoryGirl::Declaration::Static.new(:email, 'value')) - child.ensure_compiled - child.attributes.size.should == 2 - end - - it "should allow to override parent attributes" do - child = FactoryGirl::Factory.new(:child, :parent => @factory.name) - @child_declaration = FactoryGirl::Declaration::Static.new(@parent_attr, 'overridden') - child.declare_attribute(@child_declaration) - child.ensure_compiled - child.attributes.size.should == 1 - child.attributes.first.should == @child_declaration.to_attributes.first - end - - it "should allow to use parent attributes in defining additional attributes" do - User.class_eval { attr_accessor :name, :email } - - child = FactoryGirl::Factory.new(:child, :parent => @factory.name) - @child_declaration = FactoryGirl::Declaration::Dynamic.new(:email, lambda {|u| "#{u.name}@example.com"}) - child.declare_attribute(@child_declaration) - child.ensure_compiled - child.attributes.size.should == 2 - - result = child.run(FactoryGirl::Proxy::Build, {}) - result.email.should == 'value@example.com' - end - end - - it "compiles when looking for attributes" do - @factory.declare_attribute(FactoryGirl::Declaration::Static.new("name", "value")) - @factory.attributes.size.should == 1 - end end describe FactoryGirl::Factory, "when defined with a custom class" do