Skip to content

Commit

Permalink
Remove associate method from all the FactoryGirl::Proxy subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed Nov 18, 2011
1 parent 85d4735 commit 3ed2f62
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 96 deletions.
4 changes: 2 additions & 2 deletions lib/factory_girl/attribute.rb
Expand Up @@ -55,9 +55,9 @@ def ensure_non_attribute_writer!

def set_proxy_value(proxy, value)
if @ignored
proxy.set_ignored(name, value)
proxy.set_ignored(self, value)
else
proxy.set(name, value)
proxy.set(self, value)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/factory_girl/attribute/association.rb
Expand Up @@ -10,7 +10,7 @@ def initialize(name, factory, overrides)
end

def add_to(proxy)
proxy.associate(name, @factory, @overrides)
proxy.set(self, proxy.association(@factory, @overrides))
end

def association?
Expand Down
5 changes: 1 addition & 4 deletions lib/factory_girl/proxy.rb
Expand Up @@ -19,10 +19,7 @@ def set(attribute, value)
end

def set_ignored(attribute, value)
@ignored_attributes[attribute] = value
end

def associate(name, factory, attributes)
@ignored_attributes[attribute.name] = value
end

def run_callbacks(name)
Expand Down
4 changes: 3 additions & 1 deletion lib/factory_girl/proxy/attributes_for.rb
Expand Up @@ -11,7 +11,9 @@ def get(attribute)
end

def set(attribute, value)
@hash[attribute] = value
return if attribute.is_a? Attribute::Association

@hash[attribute.name] = value
end

def result(to_create)
Expand Down
6 changes: 1 addition & 5 deletions lib/factory_girl/proxy/build.rb
Expand Up @@ -15,11 +15,7 @@ def get(attribute)
end

def set(attribute, value)
@instance.send(:"#{attribute}=", value)
end

def associate(name, factory_name, overrides)
set(name, association(factory_name, overrides))
@instance.send(:"#{attribute.name}=", value)
end

def association(factory_name, overrides = {})
Expand Down
6 changes: 1 addition & 5 deletions lib/factory_girl/proxy/stub.rb
Expand Up @@ -55,11 +55,7 @@ def get(attribute)
end

def set(attribute, value)
@instance.send(:"#{attribute}=", value)
end

def associate(name, factory_name, overrides)
set(name, association(factory_name, overrides))
@instance.send(:"#{attribute.name}=", value)
end

def association(factory_name, overrides = {})
Expand Down
8 changes: 5 additions & 3 deletions spec/factory_girl/attribute/association_spec.rb
Expand Up @@ -12,10 +12,12 @@
its(:name) { should == name }
its(:factory) { should == factory }

it "tells the proxy to create an association when being added" do
proxy.stubs(:associate)
it "tells the proxy to set the association when being added" do
association = stub("association")
proxy.stubs(:set => nil, :association => association)
subject.add_to(proxy)
proxy.should have_received(:associate).with(name, factory, overrides)
proxy.should have_received(:set).with(subject, association)
proxy.should have_received(:association).with(factory, overrides)
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/factory_girl/attribute/dynamic_spec.rb
Expand Up @@ -14,7 +14,7 @@

it "calls the block to set a value" do
subject.add_to(proxy)
proxy.should have_received(:set).with(name, "value")
proxy.should have_received(:set).with(subject, "value")
end
end

Expand All @@ -23,7 +23,7 @@

it "yields the proxy to the block" do
subject.add_to(proxy)
proxy.should have_received(:set).with(name, proxy)
proxy.should have_received(:set).with(subject, proxy)
end
end

Expand All @@ -37,7 +37,7 @@

it "evaluates the attribute from the proxy" do
subject.add_to(proxy)
proxy.should have_received(:set).with(name, result)
proxy.should have_received(:set).with(subject, result)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/factory_girl/attribute/sequence_spec.rb
Expand Up @@ -14,6 +14,6 @@
it "assigns the next value in the sequence" do
proxy.stubs(:set)
subject.add_to(proxy)
proxy.should have_received(:set).with(name, "Name 5")
proxy.should have_received(:set).with(subject, "Name 5")
end
end
2 changes: 1 addition & 1 deletion spec/factory_girl/attribute/static_spec.rb
Expand Up @@ -12,7 +12,7 @@
it "sets its static value on a proxy" do
proxy.stubs(:set)
subject.add_to(proxy)
proxy.should have_received(:set).with(name, value)
proxy.should have_received(:set).with(subject, value)
end
end

Expand Down
8 changes: 5 additions & 3 deletions spec/factory_girl/declaration/implicit_spec.rb
Expand Up @@ -20,9 +20,11 @@
end

it "associates the factory" do
proxy.stubs(:associate)
association = stub("association")
proxy.stubs(:set => nil, :association => association)
attribute.add_to(proxy)
proxy.should have_received(:associate).with(name, name, {})
proxy.should have_received(:set).with(attribute, association)
proxy.should have_received(:association).with(name, {})
end
end

Expand All @@ -37,7 +39,7 @@
it "generates the sequence" do
proxy.stubs(:set)
attribute.add_to(proxy)
proxy.should have_received(:set).with(name, "magic")
proxy.should have_received(:set).with(attribute, "magic")
end
end
end
20 changes: 3 additions & 17 deletions spec/factory_girl/proxy/attributes_for_spec.rb
Expand Up @@ -11,32 +11,18 @@
subject.result(nil).should be_kind_of(Hash)
end

context "after associating a factory" do
let(:attribute) { :owner }

before { subject.associate(attribute, :user, {}) }

it "doesn't set that key in the resulting hash" do
subject.result(nil).should_not have_key(attribute)
end

it "returns nil when asked for that attribute" do
subject.get(attribute).should be_nil
end
end

describe "after setting an attribute" do
let(:attribute) { :attribute }
let(:attribute) { stub("attribute", :name => :attribute) }
let(:value) { "value" }

before { subject.set(attribute, value) }

it "sets that value in the resulting hash" do
subject.result(nil)[attribute].should == value
subject.result(nil)[:attribute].should == value
end

it "returns that value when asked for that attribute" do
subject.get(attribute).should == value
subject.get(:attribute).should == value
end
end
end
Expand Down
85 changes: 35 additions & 50 deletions spec/support/shared_examples/proxy.rb
@@ -1,91 +1,76 @@
shared_examples_for "proxy without association support" do
it "doesn't raise when asked to associate with another factory" do
expect { subject.associate(:owner, :user, {}) }.to_not raise_error
end
let(:attribute) { FactoryGirl::Attribute::Association.new(:user, :user, {}) }

it "does not call FactoryGirl.create when building an association" do
FactoryGirl.stubs(:create)
subject.association(:user)
subject.set(attribute, "awesome")
FactoryGirl.should have_received(:create).never
end

it "returns nil when building an association" do
subject.set(:association, 'x')
subject.association(:user).should be_nil
it "returns nil when accessing an association" do
subject.set(attribute, "awesome")
subject.get(:user).should be_nil
end

it "does not attempt to look up the factory when accessing the association" do
FactoryGirl.stubs(:factory_by_name)
subject.association(:awesome)
FactoryGirl.should have_received(:factory_by_name).never
end
end

shared_examples_for "proxy with association support" do |factory_girl_proxy_class|
let(:factory_name) { :user }
let(:association_name) { :owner }
let(:factory) { stub("associate_factory") }
let(:overrides) { { :one => 1, :two => 2 } }
let(:factory) { stub("associate_factory") }
let(:overrides) { { :great => "value" } }
let(:factory_name) { :author }

before do
FactoryGirl.stubs(:factory_by_name => factory)
instance.stubs(association_name => factory_name)
factory.stubs(:run => factory_name)
subject.stubs(:set)
end

it "sets a value for the association" do
subject.associate(association_name, factory_name, {})
subject.result(nil).send(association_name).should == factory_name
end

it "sets the association attribute as the factory" do
subject.associate(association_name, factory_name, {})
subject.should have_received(:set).with(association_name, factory_name)
factory.stubs(:run)
end

it "runs the factory with the correct proxy class" do
subject.associate(association_name, factory_name, {})
factory.should have_received(:run).with(factory_girl_proxy_class, {})
it "runs the factory with the correct overrides" do
subject.association(factory_name, overrides)
factory.should have_received(:run).with(factory_girl_proxy_class, overrides)
end

it "runs the factory with the correct proxy class and overrides" do
subject.associate(association_name, factory_name, overrides)
factory.should have_received(:run).with(factory_girl_proxy_class, overrides)
it "finds the factory with the correct factory name" do
subject.association(factory_name, overrides)
FactoryGirl.should have_received(:factory_by_name).with(factory_name)
end
end

shared_examples_for "proxy with :method => :build" do |factory_girl_proxy_class|
let(:factory_name) { :user }
let(:association_name) { :owner }
let(:factory) { stub("associate_factory") }
let(:overrides) { { :method => :build } }
let(:factory) { stub("associate_factory") }
let(:overrides) { { :method => :build, :great => "value" } }
let(:factory_name) { :author }

before do
FactoryGirl.stubs(:factory_by_name => factory)
instance.stubs(association_name => factory_name)
factory.stubs(:run => factory_name)
subject.stubs(:set)
factory.stubs(:run)
end

it "sets a value for the association" do
subject.associate(association_name, factory_name, overrides)
subject.result(nil).send(association_name).should == factory_name
it "runs the factory with the correct overrides" do
subject.association(factory_name, overrides)
factory.should have_received(:run).with(factory_girl_proxy_class, { :great => "value" })
end

it "sets the association attribute as the factory" do
subject.associate(association_name, factory_name, overrides)
subject.should have_received(:set).with(association_name, factory_name)
end

it "runs the factory with the correct proxy class" do
subject.associate(association_name, factory_name, overrides)
factory.should have_received(:run).with(factory_girl_proxy_class, {})
it "finds the factory with the correct factory name" do
subject.association(factory_name, overrides)
FactoryGirl.should have_received(:factory_by_name).with(factory_name)
end
end

shared_examples_for "proxy with standard getters and setters" do |attribute, value|
let(:attribute_instance) { stub("attribute #{attribute}", :name => attribute) }

before do
instance.stubs(:"#{attribute}=" => value, :"#{attribute}" => value)
end

describe "when setting an attribute" do
before do
subject.set(attribute, value)
subject.set(attribute_instance, value)
end

its(attribute) { should == value }
Expand All @@ -94,7 +79,7 @@

describe "when setting an ignored attribute" do
before do
subject.set_ignored(attribute, value)
subject.set_ignored(attribute_instance, value)
end

it { instance.should have_received(:"#{attribute}=").with(value).never }
Expand Down

0 comments on commit 3ed2f62

Please sign in to comment.