FixtureReplacement (version 3.0.1)

What is FixtureReplacement

FixtureReplacement is a Rails plugin
that provides a simple way to quickly populate your test database with
model objects without having to manage multiple, brittle fixture files. You
can easily set up complex object graphs (with models which reference other
models) and add new objects on the fly.

Not only can FixtureReplacement make your test data easier to maintain, it
can also help to make your tests and specs much more readable and
intention-revealing by allowing you to omit extraneous details and focus
only on the attributes that are important for a particular behaviour. It
works well with both RSpec and Test::Unit.

What's new since 2.0:

default_* is gone in favor of new_*.

Cyclic dependencies are no longer an issue. The “overrides hash” (the hash
passed to new_* or create_*) can now be processed.

See CHANGELOG.rdoc + test suite for further changes.

Installation

Install the plugin:

git://github.com/smtlaissezfaire/fixturereplacement.git

Using it with RSpec

Add the following to your spec/spec_helper.rb file, in the
configuration section:

you can perform arbitrary set-up and execute any Ruby code prior to
returning the hash (as shown here where a password is generated
and then used for both the :password and
:password_confirmation attributes)

a new_modelname method is automatically provided that allows you
to set up dependent model objects (in this case an instance of the
Bar model)

Available methods

Based on the above definition FixtureReplacement makes the following
methods available:

random_string: generates a random string as shown above

new_user: equivalent to User.new with the attributes for
the user.

create_user: equivalent to User.create! with the
user's attributes.

valid_user_attributes: returns a hash of the user's attributes
including associations, specified in db/example_data.rb.

Overriding attributes

Overrides of specific attributes can be performed as follows:

new_user(:thing=>"overridden")create_user(:thing=>"overridden")

Overrides can also be used with associations:

scott=create_user(:username=>"scott")post=create_post(:user=>scott)

attr_protected / attr_accessible

In the case that the model has an attr_protected field, FixtureReplacement
will assign the field as if it wasn't protected, which is convenient
for testing: