Permalink
Browse files

Clean up attribute and core factory specs

  • Loading branch information...
1 parent 6bc3b87 commit 7c9254a452fb8beb7a197fa52848b49758077c2c @joshuaclayton joshuaclayton committed Aug 13, 2011
@@ -1,8 +1,8 @@
module FactoryGirl
-
class << self
attr_accessor :aliases #:nodoc:
end
+
self.aliases = [
[/(.+)_id/, '\1'],
[/(.*)/, '\1_id']
@@ -13,8 +13,6 @@ def self.aliases_for(attribute) #:nodoc:
pattern, replace = *params
if pattern.match(attribute.to_s)
attribute.to_s.sub(pattern, replace).to_sym
- else
- nil
end
end.compact << attribute
end
@@ -14,13 +14,7 @@ class Attribute #:nodoc:
def initialize(name)
@name = name.to_sym
-
- if @name.to_s =~ /=$/
- attribute_name = $`
- raise AttributeDefinitionError,
- "factory_girl uses 'f.#{attribute_name} value' syntax " +
- "rather than 'f.#{attribute_name} = value'"
- end
+ ensure_non_attribute_writer!
end
def add_to(proxy)
@@ -43,6 +37,15 @@ def <=>(another)
self.priority <=> another.priority
end
- end
+ private
+ def ensure_non_attribute_writer!
+ if @name.to_s =~ /=$/
+ attribute_name = $`
+ raise AttributeDefinitionError,
+ "factory_girl uses 'f.#{attribute_name} value' syntax " +
+ "rather than 'f.#{attribute_name} = value'"
+ end
+ end
+ end
end
@@ -1,8 +1,6 @@
module FactoryGirl
class Attribute #:nodoc:
-
class Association < Attribute #:nodoc:
-
attr_reader :factory
def initialize(name, factory, overrides)
@@ -19,6 +17,5 @@ def association?
true
end
end
-
end
end
@@ -1,6 +1,5 @@
module FactoryGirl
class Attribute #:nodoc:
-
class Callback < Attribute #:nodoc:
def initialize(name, block)
@name = name.to_sym
@@ -11,6 +10,5 @@ def add_to(proxy)
proxy.add_callback(name, @block)
end
end
-
end
end
@@ -1,6 +1,5 @@
module FactoryGirl
class Attribute #:nodoc:
-
class Dynamic < Attribute #:nodoc:
def initialize(name, block)
super(name)
@@ -15,6 +14,5 @@ def add_to(proxy)
proxy.set(name, value)
end
end
-
end
end
@@ -9,9 +9,9 @@ class Sequence
attr_reader :name
def initialize(name, value = 1, &proc) #:nodoc:
- @name = name
+ @name = name
@proc = proc
- @value = value || 1
+ @value = value
end
def next
@@ -1,33 +1,31 @@
require 'spec_helper'
-describe Factory, "aliases" do
-
- it "should include an attribute as an alias for itself by default" do
- FactoryGirl.aliases_for(:test).should include(:test)
+describe FactoryGirl, "aliases" do
+ context "aliases for an attribute" do
+ subject { FactoryGirl.aliases_for(:test) }
+ it { should include(:test) }
+ it { should include(:test_id) }
end
- it "should include the root of a foreign key as an alias by default" do
- FactoryGirl.aliases_for(:test_id).should include(:test)
+ context "aliases for a foreign key" do
+ subject { FactoryGirl.aliases_for(:test_id) }
+ it { should include(:test) }
+ it { should include(:test_id) }
end
- it "should include an attribute's foreign key as an alias by default" do
- FactoryGirl.aliases_for(:test).should include(:test_id)
+ context "aliases for an attribute starting with an underscore" do
+ subject { FactoryGirl.aliases_for(:_id) }
+ it { should_not include(:id) }
end
+end
- it "should NOT include an attribute as an alias when it starts with underscore" do
- FactoryGirl.aliases_for(:_id).should_not include(:id)
+describe Factory, "after defining an alias" do
+ before do
+ Factory.alias(/(.*)_suffix/, '\1')
end
- describe "after adding an alias" do
-
- before do
- Factory.alias(/(.*)_suffix/, '\1')
- end
-
- it "should return the alias in the aliases list" do
- FactoryGirl.aliases_for(:test_suffix).should include(:test)
- end
-
- end
+ subject { FactoryGirl.aliases_for(:test_suffix) }
+ it { should include(:test) }
+ it { should include(:test_suffix_id) }
end
@@ -1,32 +1,25 @@
require 'spec_helper'
describe FactoryGirl::Attribute::Association do
- before do
- @name = :author
- @factory = :user
- @overrides = { :first_name => 'John' }
- @attr = FactoryGirl::Attribute::Association.new(@name, @factory, @overrides)
- end
+ let(:name) { :author }
+ let(:factory) { :user }
+ let(:overrides) { { :first_name => "John" } }
+ let(:proxy) { stub("proxy") }
- it "should have a name" do
- @attr.name.should == @name
- end
+ subject { FactoryGirl::Attribute::Association.new(name, factory, overrides) }
- it "is an association" do
- @attr.should be_association
- end
+ it { should be_association }
+ its(:name) { should == name }
+ its(:factory) { should == factory }
- it "should have a factory" do
- @attr.factory.should == @factory
- end
-
- it "should tell the proxy to associate when being added to a proxy" do
- proxy = stub("proxy", :associate => nil)
- @attr.add_to(proxy)
- proxy.should have_received(:associate).with(@name, @factory, @overrides)
+ it "tells the proxy to create an association when being added" do
+ proxy.stubs(:associate)
+ subject.add_to(proxy)
+ proxy.should have_received(:associate).with(name, factory, overrides)
end
+end
- it "should convert names to symbols" do
- FactoryGirl::Attribute::Association.new('name', :user, {}).name.should == :name
- end
+describe FactoryGirl::Attribute::Association, "with a string name" do
+ subject { FactoryGirl::Attribute::Association.new("name", :user, {}) }
+ its(:name) { should == :name }
end
@@ -1,23 +1,22 @@
require 'spec_helper'
describe FactoryGirl::Attribute::Callback do
- before do
- @name = :after_create
- @block = proc{ 'block' }
- @attr = FactoryGirl::Attribute::Callback.new(@name, @block)
- end
+ let(:name) { :after_create }
+ let(:block) { proc { "block" } }
+ let(:proxy) { stub("proxy") }
- it "should have a name" do
- @attr.name.should == @name
- end
+ subject { FactoryGirl::Attribute::Callback.new(name, block) }
- it "should set its callback on a proxy" do
- proxy = stub("proxy", :add_callback => true)
- @attr.add_to(proxy)
- proxy.should have_received(:add_callback).with(@name, @block)
- end
+ its(:name) { should == name }
- it "should convert names to symbols" do
- FactoryGirl::Attribute::Callback.new('name', nil).name.should == :name
+ it "set its callback on a proxy" do
+ proxy.stubs(:add_callback)
+ subject.add_to(proxy)
+ proxy.should have_received(:add_callback).with(name, block)
end
end
+
+describe FactoryGirl::Attribute::Callback, "with a string name" do
+ subject { FactoryGirl::Attribute::Callback.new("name", nil) }
+ its(:name) { should == :name }
+end
@@ -1,56 +1,56 @@
require 'spec_helper'
describe FactoryGirl::Attribute::Dynamic do
- before do
- @name = :first_name
- @block = lambda { 'value' }
- @attr = FactoryGirl::Attribute::Dynamic.new(@name, @block)
- end
+ let(:name) { :first_name }
+ let(:proxy) { stub("proxy", :set => nil) }
+ let(:block) { lambda { } }
- it "should have a name" do
- @attr.name.should == @name
- end
+ subject { FactoryGirl::Attribute::Dynamic.new(name, block) }
- it "should call the block to set a value" do
- @proxy = stub("proxy", :set => nil)
- @attr.add_to(@proxy)
- @proxy.should have_received(:set).with(@name, 'value')
- end
+ its(:name) { should == name }
- it "should yield the proxy to the block when adding its value to a proxy" do
- @block = lambda {|a| a }
- @attr = FactoryGirl::Attribute::Dynamic.new(:user, @block)
- @proxy = stub("proxy", :set => nil)
- @attr.add_to(@proxy)
- @proxy.should have_received(:set).with(:user, @proxy)
- end
+ context "with a block returning a static value" do
+ let(:block) { lambda { "value" } }
- it "evaluates the block with in the context of the proxy without an argument" do
- result = 'other attribute value'
- @block = lambda { other_attribute }
- @attr = FactoryGirl::Attribute::Dynamic.new(:user, @block)
- @proxy = stub("proxy", :set => nil, :other_attribute => result)
- @attr.add_to(@proxy)
- @proxy.should have_received(:set).with(:user, result)
+ it "calls the block to set a value" do
+ subject.add_to(proxy)
+ proxy.should have_received(:set).with(name, "value")
+ end
end
- it "should raise an error when defining an attribute writer" do
- lambda {
- FactoryGirl::Attribute::Dynamic.new('test=', nil)
- }.should raise_error(FactoryGirl::AttributeDefinitionError)
+ context "with a block returning its block-level variable" do
+ let(:block) { lambda {|thing| thing } }
+
+ it "yields the proxy to the block" do
+ subject.add_to(proxy)
+ proxy.should have_received(:set).with(name, proxy)
+ end
end
- it "should raise an error when returning a sequence" do
- Factory.stubs(:sequence => FactoryGirl::Sequence.new(:email))
- block = lambda { Factory.sequence(:email) }
- attr = FactoryGirl::Attribute::Dynamic.new(:email, block)
- proxy = stub("proxy")
- lambda {
- attr.add_to(proxy)
- }.should raise_error(FactoryGirl::SequenceAbuseError)
+ context "with a block referencing an attribute on the proxy" do
+ let(:block) { lambda { attribute_defined_on_proxy } }
+ let(:result) { "other attribute value" }
+
+ before do
+ proxy.stubs(:attribute_defined_on_proxy => result)
+ end
+
+ it "evaluates the attribute from the proxy" do
+ subject.add_to(proxy)
+ proxy.should have_received(:set).with(name, result)
+ end
end
- it "should convert names to symbols" do
- FactoryGirl::Attribute::Dynamic.new('name', nil).name.should == :name
+ context "with a block returning a sequence" do
+ let(:block) { lambda { Factory.sequence(:email) } }
+
+ it "raises a sequence abuse error" do
+ expect { subject.add_to(proxy) }.to raise_error(FactoryGirl::SequenceAbuseError)
+ end
end
end
+
+describe FactoryGirl::Attribute::Dynamic, "with a string name" do
+ subject { FactoryGirl::Attribute::Dynamic.new("name", nil) }
+ its(:name) { should == :name }
+end
Oops, something went wrong.

0 comments on commit 7c9254a

Please sign in to comment.