diff --git a/GETTING_STARTED.md b/GETTING_STARTED.md index 65fff81..916e697 100644 --- a/GETTING_STARTED.md +++ b/GETTING_STARTED.md @@ -788,6 +788,28 @@ FactoryGirl.json(:user) Finally, you can override factory\_girl's own strategies if you'd like by registering a new object in place of the strategies. +Custom Methods to Persist Objects +------------------------------ + +By default, creating a record will call `save!` on the instance; since this +may not always be ideal, you can override that behavior by defining +`to_create` on the factory: + +```ruby +factory :different_orm_model do + to_create {|instance| instance.persist! } +end +``` + +To disable the persistence method altogether on create, you can `skip_create` +for that factory: + +```ruby +factory :user_without_database do + skip_create +end +``` + Cucumber Integration -------------------- diff --git a/lib/factory_girl/definition_proxy.rb b/lib/factory_girl/definition_proxy.rb index bd0f2c0..3471a64 100644 --- a/lib/factory_girl/definition_proxy.rb +++ b/lib/factory_girl/definition_proxy.rb @@ -144,6 +144,10 @@ module FactoryGirl @definition.to_create(&block) end + def skip_create + @definition.to_create {|instance| } + end + def factory(name, options = {}, &block) @child_factories << [name, options, block] end diff --git a/spec/acceptance/skip_create_spec.rb b/spec/acceptance/skip_create_spec.rb new file mode 100644 index 0000000..deda4ee --- /dev/null +++ b/spec/acceptance/skip_create_spec.rb @@ -0,0 +1,19 @@ +require "spec_helper" + +describe "skipping the default create" do + before do + define_model("User", email: :string) + + FactoryGirl.define do + factory :user do + skip_create + + email "john@example.com" + end + end + end + + it "doesn't execute anything when creating the instance" do + FactoryGirl.create(:user).should_not be_persisted + end +end