Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow methods to be called from the instance in factory girl attributes
This fixes a regression introduced with the introduction of the anonymous class. Closes #264
- Loading branch information
1 parent
4b6ada7
commit d918c1d
Showing
3 changed files
with
69 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require "spec_helper" | ||
|
||
describe "calling methods on the model instance" do | ||
before do | ||
define_model('User', :age => :integer, :age_copy => :integer) do | ||
def age | ||
read_attribute(:age) || 18 | ||
end | ||
end | ||
|
||
FactoryGirl.define do | ||
factory :user do | ||
age_copy { age } | ||
end | ||
end | ||
end | ||
|
||
context "without the attribute being overridden" do | ||
it "returns the correct value from the instance" do | ||
FactoryGirl.build(:user).age_copy.should == 18 | ||
end | ||
|
||
it "returns nil during attributes_for" do | ||
FactoryGirl.attributes_for(:user)[:age_copy].should be_nil | ||
end | ||
|
||
it "doesn't instantiate a record with attributes_for" do | ||
User.stubs(:new) | ||
FactoryGirl.attributes_for(:user) | ||
User.should have_received(:new).never | ||
end | ||
end | ||
|
||
context "with the attribute being overridden" do | ||
it "uses the overridden value" do | ||
FactoryGirl.build(:user, :age_copy => nil).age_copy.should be_nil | ||
end | ||
|
||
it "uses the overridden value during attributes_for" do | ||
FactoryGirl.attributes_for(:user, :age_copy => 25)[:age_copy].should == 25 | ||
end | ||
end | ||
|
||
context "with the referenced attribute being overridden" do | ||
it "uses the overridden value" do | ||
FactoryGirl.build(:user, :age => nil).age_copy.should be_nil | ||
end | ||
|
||
it "uses the overridden value during attributes_for" do | ||
FactoryGirl.attributes_for(:user, :age => 25)[:age_copy].should == 25 | ||
end | ||
end | ||
end |