Skip to content

Commit

Permalink
Allow setting id for build_stubbed objects
Browse files Browse the repository at this point in the history
Closes #451
  • Loading branch information
joshuaclayton committed Nov 7, 2012
1 parent fc14baf commit e85c008
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/factory_girl/strategy/stub.rb
Expand Up @@ -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?
Expand Down
14 changes: 14 additions & 0 deletions spec/acceptance/build_stubbed_spec.rb
Expand Up @@ -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.