Permalink
Browse files

Remove associate method from all the FactoryGirl::Proxy subclasses

  • Loading branch information...
1 parent 85d4735 commit 3ed2f6274a8bf5d8c4efa8d3f45b16025c0bec20 @joshuaclayton joshuaclayton committed Nov 1, 2011
@@ -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
@@ -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?
@@ -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)
@@ -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)
@@ -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 = {})
@@ -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 = {})
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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 }
@@ -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 }

0 comments on commit 3ed2f62

Please sign in to comment.