-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up attribute and core factory specs
- Loading branch information
1 parent
6bc3b87
commit 7c9254a
Showing
21 changed files
with
342 additions
and
479 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.