Permalink
Browse files

Allow setting id for build_stubbed objects

Closes #451
  • Loading branch information...
1 parent fc14baf commit e85c0086aac30de511d48d73cf5bb698405c12e0 @joshuaclayton joshuaclayton committed Nov 7, 2012
Showing with 15 additions and 1 deletion.
  1. +1 −1 lib/factory_girl/strategy/stub.rb
  2. +14 −0 spec/acceptance/build_stubbed_spec.rb
View
2 lib/factory_girl/strategy/stub.rb
@@ -21,7 +21,7 @@ def next_id
end
def stub_database_interaction_on_result(result_instance)
- result_instance.id = next_id
+ result_instance.id ||= next_id
result_instance.instance_eval do
def persisted?
View
14 spec/acceptance/build_stubbed_spec.rb
@@ -134,3 +134,17 @@
expect { build_stubbed(:thing_without_timestamp, :created_at => Time.now) }.to raise_error(NoMethodError, /created_at=/)
end
end
+
+describe 'defaulting `id`' do
+ before do
+ define_model('Post')
+
+ FactoryGirl.define do
+ factory :post
+ end
+ end
+
+ it 'allows overriding id' do
+ FactoryGirl.build_stubbed(:post, id: 12).id.should eq 12
+ end
+end

0 comments on commit e85c008

Please sign in to comment.