1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

Allow a user to skip the default create execution

Closes #340
This commit is contained in:
Joshua Clayton 2012-04-23 22:40:05 -05:00
parent c52c3d06fc
commit 351b04de02
3 changed files with 45 additions and 0 deletions

View file

@ -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
--------------------

View file

@ -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

View file

@ -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