• Stars
    star
    17
  • Rank 1,257,181 (Top 25 %)
  • Language
    Ruby
  • License
    GNU General Publi...
  • Created over 16 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

FixtureReplacement rails plugin

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 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¶ ↑

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:

  • A ‘random_string’ method is provided for attributes whose exact value isn’t important; this means you can create multiple, unique model instances

  • 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)

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 ([email protected])

More Repositories

1

ndb

The node.js debugger
JavaScript
183
star
2

bcompiler

Bootstrap compiler
78
star
3

fast_git_deploy

Cap tasks to speed up cap deploys with git instead of symlinking (as described in http://github.com/blog/470-deployment-script-spring-cleaning)
Ruby
46
star
4

guillotine

A Ruby SQL Parser
Ruby
28
star
5

newrelic_rpm

git-svn clone of http://svn.newrelic.com/rpm/agent/newrelic_rpm. WARNING: THE SVN REPOS IS DEPRECATED. Use git://github.com/smtlaissezfaire/newrelic_new_mirror.git instead.
Ruby
28
star
6

git-shell-scripts

Homemade git shell scripts
21
star
7

project_management

A checklist of project management + development todos
21
star
8

css_parser

A partially completed, bug ridden CSS parser written in treetop (please fork me)
Ruby
18
star
9

12_hour_time

git svn clone of http://rails-twelve-hour-time-plugin.googlecode.com/svn/trunk/12_hour_time
Ruby
16
star
10

enhanced_query_analyzer

Just like the query_analyzer plugin, but logs to the db instead of the log file
Ruby
16
star
11

acts_as_sluggable

git-svn mirror of acts_as_sluggable rails plugin from http://code.dunae.ca/acts_as_slugable/ - although you should really be using slugify instead
16
star
12

ruby-protobuf

Mirror of ruby-protobuf over @ google code (http://ruby-protobuf.googlecode.com/svn). Updated once per hour.
Ruby
13
star
13

object_oriented_c

creating OO in pure C (not to be confused with the ooc project)
C
12
star
14

slugify

Slug generation for ActiveRecord models
Ruby
11
star
15

jm

Javascript Markup (think Markaby for Javascript, something akin to Builder)
JavaScript
11
star
16

validates_date_time

A git-svn mirror of the validates_date_time plugin, found at http://svn.viney.net.nz/things/rails/plugins/validates_date_time/
Ruby
11
star
17

cap_db_dump

Capistrano Tasks for dumping and transfering production databases
Ruby
10
star
18

earing

Zed's Earing
C
10
star
19

using

The *proper* way to load ruby files.
Ruby
10
star
20

espionage

Javascript mocking and stubbing framework
JavaScript
10
star
21

active_conductor

Conductor plugin for Rails 3
Ruby
9
star
22

protobuf-c

Google Protocol Buffers in C. git svn mirror of http://protobuf-c.googlecode.com/svn/
C
9
star
23

annotate_models_svn_clone

git-svn clone of http://repo.pragprog.com/svn/Public/plugins/annotate_models/
9
star
24

cap_unicorn

unicorn capistrano tasks
Ruby
8
star
25

peg-leg

peg/leg - recursive-descent parser generators for C
8
star
26

sup

Mirror of project over @gittorious
Ruby
8
star
27

gforth.el

gforth emacs mode from gforth distribution
8
star
28

ruby-emacs

A git-svn clone of elisp ruby files in the standard distribution. Updated hourly
Emacs Lisp
8
star
29

shunit2

shell script Unit testing framework (mirror of svn repository @ http://shunit2.googlecode.com/svn)
Shell
7
star
30

emacs-screencasts

A series of screencasts on getting Emacs working with ruby & rails
7
star
31

ruby2cext

Mirror of svn repository @ http://ruby2cext.rubyforge.org/svn/
6
star
32

cutest

Mirror of CuTest (C testing framework) repository
5
star
33

nginx-test

Spec helpers for testing nginx routing
Ruby
5
star
34

atrophy

Rails plugin to find unused partials in a rails app
Ruby
5
star
35

newrelic_new_mirror

Mirror of New Relic's new svn repository: http://newrelic.rubyforge.org/svn/newrelic_rpm
Ruby
5
star
36

acts_as_tokenizable

Ruby
5
star
37

mod_lisp

Mirror of the mod_lisp apache module svn repository (http://www.fractalconcept.com:8000/public/open-source/mod_lisp/)
C
5
star
38

hopcroft

A library for regular languages: Converting regexen into state machines
Ruby
5
star
39

labeledformhelper

git-svn mirror of technoweenie's LabeledFormHelper (from http://svn.techno-weenie.net/projects/plugins/labeled_form_helper/)
5
star
40

treetop-mode

Emacs major mode for treetop
Emacs Lisp
4
star
41

simply

A simple, fast, markaby-esque templating engine
Ruby
4
star
42

migration_collapser

Collapse already run rails migrations
Ruby
4
star
43

erlang_mode

erlang.el for emacs
Emacs Lisp
4
star
44

migrate_test_db

Git'ized version of the migrate_test_db plugin
4
star
45

mix_master

A pure ruby solution for unmixing modules.
Ruby
4
star
46

ruby-linkparser2

svn clone of svn://deveiate.org/Ruby-LinkParser2
C
4
star
47

git-rev-num

Global revision numbers for local git repositories
Ruby
4
star
48

emacs-lisp-experiments

Set of extensions for emacs
Emacs Lisp
4
star
49

action_mailer_logger

Log action mailer deliveries to the db
Ruby
4
star
50

enhanced_migrations

git-svn clone of http://enhanced-mgrtns.rubyforge.org/svn/enhanced_migrations/
4
star
51

submodule_to_braid

Script to replace git submodules with braid checkouts
4
star
52

whitespace-el

whitespace.el (whitespace-mode) for emacs
Emacs Lisp
4
star
53

lafcadio

Lafcadio is an object-relational mapping framework for Ruby. It currently supports MySQL; in the future it will support a wide range of databases. (Written by Francis Hwang. A csv mirror.)
4
star
54

nginx-conf

"life-hack" code for manual testing of nginx configuration (with rails) locally
4
star
55

wptouch

A fork of wp-touch fixing an nginx SSI issue
PHP
4
star
56

datedbackup

A small backup script creating timestamped backups with rsync.
Ruby
4
star
57

FieldAttributeBuilder

Create fields_for accessors automatically in your models.
Ruby
3
star
58

peg-leg-fork

My fork of the peg/leg project
C
3
star
59

ruby-linkparser2-fork

C
3
star
60

quick_mysql_import

Import & Export a Mysql DB using "LOAD DATA INFILE"
Ruby
3
star
61

emacs.d

Personal .emacs.d files
Emacs Lisp
3
star
62

real_world_haskell

Haskell
3
star
63

erlang_docs

A mirror of the official erlang docs found at http://www.erlang.org/doc
Shell
3
star
64

prototype_project

Ruby
3
star
65

dike

a simple memory leak detector for ruby with preconfigured rails hooks. (git-svn mirror)
3
star
66

rspec_regression

Rails plugin for generating regressions in rspec
Ruby
3
star
67

c_recl

A "repl" for C (using gcc internally)
C
3
star
68

javascript_tools_textmate_bundle

Mirror of svn repository @ (http://bundles.bundleforge.com/trunk/bundles/JavaScript%20Tools.tmbundle/)
3
star
69

git_revision_number

Rails plugin for displaying sequential git revision numbers
Ruby
3
star
70

new_excel

Something in between excel and a programming language. WIP.
Ruby
3
star
71

improved_mysql_schema_dumper

Rails plugin to dump and load complete mysql dumps with rake db:schema rails tasks. Handles mysql views.
Ruby
3
star
72

ruby_gazelle

Ruby bindings for the Gazelle lua based parser-generator
C
3
star
73

loop

a lispy javascript
JavaScript
3
star
74

balie

Java
3
star
75

geokit

git-svn mirror of http://geokit.rubyforge.org/svn/trunk (out of date)
3
star
76

willgit

Mirror of gittorius repository: git://gitorious.org/willgit/mainline.git
3
star
77

attachment_fu

Because you shouldn't delete commits from an existing project
3
star
78

annotate_models2

A fork of ctran's annotate_models that works on rails 2.0.2
Ruby
3
star
79

tagz

markaby-esq templating system. mirror of svn repos: http://codeforpeople.rubyforge.org/svn/tagz/
Ruby
3
star
80

st_johns

Papers from St. John's College
3
star
81

calendar_helper

Mirror of calendar helper plugin
Ruby
3
star
82

mysql_utilities

Ruby
3
star
83

lazy_attr

lazy_attr_reader and lazy_attr_accessor convenience methods. Similar to lazy_reader, found in the grit library
Ruby
3
star
84

etchrec

Capistrano Deployment Recipes for Ruby on Rails on the GNU/Linux Debian Etch Operating System. Uses only the dpkg manager for installation of the rails stack, while adding some useful administrative tasks.
Ruby
3
star
85

practical-common-lisp

Examples from Practical Common Lisp, by Peter Seibel
2
star
86

gems

git-svn Mirror of techoweenie's gems plugin, found at (http://svn.techno-weenie.net/projects/plugins/gems)
2
star
87

validate_fixtures

Validate your crappy rails fixtures
2
star
88

index_view

Ruby
2
star
89

CoreDataVsSqliteBenchmark

core data vs sqlite benchmarks
Objective-C
2
star
90

prototype_node_project

A prototype repository for all (of my) javascript projects.
JavaScript
2
star
91

krc

Examples from "The C Programming Language"
C
2
star
92

cgreen

A mirror of the cgreen repository
2
star
93

multipart_mime

Ruby
2
star
94

little-schemer

Scheme
2
star
95

active_record_attribute_parser

Parse hash query params generated by rails' form helpers (i.e. (1i), (2i), etc)
Ruby
2
star
96

apache_configs

2
star
97

mysql_collations

Show the collations of your mysql tables in a rails project
2
star
98

yasnippet

(outdated) Mirror of yasnippet svn repos
2
star
99

mysql_multi_insert

Insert multiple records in one INSERT statement
Ruby
2
star
100

git-cap-tasks

Stolen from Bryan Helmkamp
Ruby
2
star