Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now able to specify :method => :build in a factory's association.
See issue #64.
- Loading branch information
Jim Kingdon
committed
Aug 25, 2011
1 parent
d22e0d8
commit 9037481
Showing
10 changed files
with
226 additions
and
8 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require 'spec_helper' | ||
|
||
describe "a stubbed instance" do | ||
include FactoryGirl::Syntax::Methods | ||
|
||
before do | ||
define_model('User') | ||
|
||
define_model('Post', :user_id => :integer) do | ||
belongs_to :user | ||
end | ||
|
||
FactoryGirl.define do | ||
factory :user | ||
|
||
factory :post do | ||
user | ||
end | ||
end | ||
end | ||
|
||
subject { build_stubbed(:post) } | ||
|
||
it "acts as if it came from the database" do | ||
should_not be_new_record | ||
end | ||
|
||
it "assigns associations and acts as if it is saved" do | ||
subject.user.should be_kind_of(User) | ||
subject.user.should_not be_new_record | ||
end | ||
end | ||
|
||
describe "a stubbed instance with :method => :build" do | ||
include FactoryGirl::Syntax::Methods | ||
|
||
before do | ||
define_model('User') | ||
|
||
define_model('Post', :user_id => :integer) do | ||
belongs_to :user | ||
end | ||
|
||
FactoryGirl.define do | ||
factory :user | ||
|
||
factory :post do | ||
association(:user, :method => :build) | ||
end | ||
end | ||
end | ||
|
||
subject { build_stubbed(:post) } | ||
|
||
it "acts as if it is saved in the database" do | ||
should_not be_new_record | ||
end | ||
|
||
it "assigns associations and acts as if it is saved" do | ||
subject.user.should be_kind_of(User) | ||
subject.user.should_not be_new_record | ||
end | ||
|
||
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
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
9037481
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious why not just use the method used when the Factory was invoked? If I say build, build it and all associations, if I say create, create the entire hierarchy. As it is now I'd have to have separately factories if I want to be able to create and build.
9037481
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
9037481
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this got merged without me taking a look over it. @NZKoz had suggested
:method => :build
but I think it should be inferred from the way the factory is called (create
,build
, orbuild_stubbed
). I'm not going to revert this commit, since it's a step in the right direction, but I'll talk to @jferris and try to get a better grasp why it can't be inferred, if that's actually the case.9037481
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joshuaclayton Did you find out anything on if the method can be inferred? The comment added to Proxy::Create in this commit suggests that not overriding the method has no impact, and that may or may not be true in ActiveRecord, but it certainly has an impact in other ORMs like Mongoid.
9037481
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes indeed, this doesn't really help because now you have to create two different factories (one for where the association is 'normal' and another one where you state :method => :build).
Still, the foreign key validation problem when you want to save the build model is not solved (validates_presence_of :user_id fails because the associated model user doesn't have an id yet).