Method: Radius::Spec::ModelFactory.create
- Defined in:
- lib/radius/spec/model_factory.rb
.create(name, custom_attrs = {}, &block) ⇒ Object
Convenience wrapper for building, and persisting, a model template.
This is a thin wrapper around build(name, attrs).tap(&:save!). The
persistence message save! will only be called on objects which
respond to it.
Avoid for New Code
It is strongly suggested that you avoid using create for new code.
Instead be explicit about when and how objects are persisted. This
allows you to have fine grain control over how your data is setup.
We suggest that you create instances which need to be persisted before your specs using the following syntax:
let(:an_instance) { build("AnyClass") }
before do
an_instance.save!
end
Alternatively if you really want for the instance be lazy instantiated, and persisted, pass the appropriate persistence method as the block:
let(:an_instance) { build("AnyClass", &:save!) }
456 457 458 459 460 |
# File 'lib/radius/spec/model_factory.rb', line 456 def create(name, custom_attrs = {}, &block) instance = build(name, custom_attrs, &block) instance.save! if instance.respond_to?(:save!) instance end |