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!) }

Parameters:

  • name (Class, String, Symbol)

    fully qualified domain model class name or constant

  • custom_attrs (Hash{String,Symbol => Object}) (defaults to: {})

    hash of custom attributes to replace registered template default values

  • block

    optional block which is passed through to new when instantiating name

Returns:

  • instance of name instantiated with custom_attrs and the registered template attributes

Raises:

See Also:

Since:

  • 0.1.0



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