
Security News
CISA’s 2025 SBOM Guidance Adds Hashes, Licenses, Tool Metadata, and Context
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.
= FixtureReplacement
== What is FixtureReplacement?
FixtureReplacement is a Rails gem 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.
(If you've ever used FactoryGirl / FactoryBot, you're probably already quite familiar with fixture_replacement, as it proceeded FactoryGirl. Also, FR is much more opinionated, and much less PC!)
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[http://rspec.rubyforge.org/] and Test::Unit[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html].
== What's new since 2.0:
See CHANGELOG.rdoc + test suite for further changes.
== Installation
Add it to your Gemfile:
group :development, :test do gem "fixture_replacement", "~> 4.0" end
=== Using it with RSpec
Add the following to your spec/rails_helper.rb file, in the configuration section:
RSpec.configure do |config| config.include FixtureReplacement end
=== Using it with Test::Unit
Add the following to your test/test_helper.rb file:
class Test::Unit::TestCase include FixtureReplacement end
== How to use FixtureReplacement
=== Defining default attributes
At the heart of FixtureReplacement is the db/example_data.rb file where you define the default attributes for each of your test models. This example shows the default attributes for a user:
module FixtureReplacement
attributes_for :user do |u|
password = random_string
u.value = "a value",
u.other = "other value",
u.another = random_string, # random string 10 characters long
u.one_more = random_string(15), # 15 characters long
u.password = password,
u.password_confirmation = password,
u.associated_object = new_bar # expects attributes_for :bar to be setup
end
end
Note that:
=== Available methods
Based on the above definition FixtureReplacement makes the following methods available:
=== 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)
=== Validate your fixtures (thanks Fixjour)
Validate your fixture definitions after including it in the spec helper or test helper:
==== spec_helper.rb:
Spec::Runner.configuration do |config| config.include FixtureReplacement end
FixtureReplacement.validate!
==== test_helper.rb
class Test::Unit::TestCase include FixtureReplacement end
FixtureReplacement.validate!
=== Using FixtureReplacement within script/console
$ ./script/console Loading development environment
include FR => Object create_user => #<User id: 1, crypted_password: "521faec1c095..." ...>
= Philosophy & Disadvantages
See philosophy_and_bugs.rdoc
= Contributors, Contributions, & BUGS
See contributions.rdoc
== License
This software is dual licensed under the MIT and the GPLv3 Licenses (it's your pick).
Copyright 2007-2022 Scott Taylor / smtlaissezfaire[http://github.com/smtlaissezfaire] (scott@railsnewbie.com)
FAQs
Unknown package
We found that fixture_replacement demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.
Security News
A clarification on our recent research investigating 60 malicious Ruby gems.
Security News
ESLint now supports parallel linting with a new --concurrency flag, delivering major speed gains and closing a 10-year-old feature request.