Skip to content

Commit

Permalink
Allow created_at to be set when using build_stubbed
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed Jun 22, 2012
1 parent b7f3789 commit b9474ce
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/factory_girl/strategy/stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,45 @@ def next_id

def stub_database_interaction_on_result(result_instance)
result_instance.id = next_id

result_instance.instance_eval do
def persisted?
!new_record?
end

def created_at
@created_at ||= Time.now
end

def new_record?
id.nil?
end

def save(*args)
raise "stubbed models are not allowed to access the database"
raise 'stubbed models are not allowed to access the database'
end

def destroy(*args)
raise "stubbed models are not allowed to access the database"
raise 'stubbed models are not allowed to access the database'
end

def connection
raise "stubbed models are not allowed to access the database"
raise 'stubbed models are not allowed to access the database'
end

def reload
raise "stubbed models are not allowed to access the database"
raise 'stubbed models are not allowed to access the database'
end

def update_attribute(*args)
raise "stubbed models are not allowed to access the database"
raise 'stubbed models are not allowed to access the database'
end
end

created_at_missing_default = result_instance.respond_to?(:created_at) && !result_instance.created_at
result_instance_missing_created_at = !result_instance.respond_to?(:created_at)

if created_at_missing_default || result_instance_missing_created_at
result_instance.instance_eval do
def created_at
@created_at ||= Time.now
end
end
end
end
Expand Down
35 changes: 35 additions & 0 deletions spec/acceptance/build_stubbed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,38 @@
end.should == expected
end
end

describe "defaulting `created_at`" do
include FactoryGirl::Syntax::Methods

before do
define_model('ThingWithTimestamp', :created_at => :datetime)
define_model('ThingWithoutTimestamp')

FactoryGirl.define do
factory :thing_with_timestamp
factory :thing_without_timestamp
end

Timecop.freeze Time.now
end

after { Timecop.return }

it "defaults created_at for objects with created_at" do
build_stubbed(:thing_with_timestamp).created_at.should == Time.now
end

it "adds created_at to objects who don't have the method" do
build_stubbed(:thing_without_timestamp).should respond_to(:created_at)
end

it "allows overriding created_at for objects with created_at" do
build_stubbed(:thing_with_timestamp, :created_at => 3.days.ago).created_at.should == 3.days.ago
end

it "doesn't allow setting created_at on an object that doesn't define it" do
expect { build_stubbed(:thing_without_timestamp, :created_at => Time.now) }.to
raise_error(NoMethodError, /created_at=/)
end
end

0 comments on commit b9474ce

Please sign in to comment.