Skip to content
Replacement for ActiveRecord fixtures using an extremely flexible ruby-based approach.
Find file
New pull request
Latest commit bb53647 @history history Correctly decrement open transactions to fix broken-ness when used wi…
…th Rails 2.3.2's new savepoint implementation of transactions
Failed to load latest commit information.
spec Revert "added ModelDefinition::StubProxy so that you can relate stubs…
tasks I don't think this is the time, nor the place
LICENSE fixture_mocking: The lauching stock of the rails testing community.
README Added example of handling many-to-many stub setup
init.rb update for edge rails with hack so ActiveRecord::Base.connection.incr…



Creates in-memory versions of models for testing.  This attempts to solve a
few problems with ActiveRecord Fixtures:

Speed - There's no hit to the database and no cleanup between tests.
Flexibility - You can define a global set of stubs, or define custom ones
  for specific tests/specs

A lot of these ideas were taken from various approaches to fixtures and mocking
that I've seen:

fixture scenarios (
unit_record (
Rspec and #mock_model

Exemplar (
I actually saw Exemplar after I wrote the initial version, but they do seem similar.

I honestly don't know if this is a better way or anything, I'm just experimenting with
a different approach.  Oh, and apparently this is an implementation of the ObjectMother 
( pattern?

  # test/unit example
  # test/unit support not fully implemented yet
  require 'model_stubbing'

  class FooTest < Test::Unit::TestCase
    define_models do
      time 2007, 6, 1
      model Role do
        stub :owner, :name => 'owner'
        stub :author, :name => 'author'
      model User do
        stub :name => 'bob', :admin => false
        stub :admin, :admin => true # inherits from default fixture
        stub :author, :roles => [all_stubs(:author_role)]
        stub :owner, :roles => [all_stubs(:author_role), all_stubs(:owner_role)]
      model Post do
        # uses admin user fixture above
        stub :title => 'initial', :user => all_stubs(:admin_user), 
          :published_at => current_time + 5.days
    def test_foo
      @user   = users(:default) # default user stub
      @admin  = users(:admin)
      @custom = users(:default, :age => 25) # custom attributes, 
                                            #but not equal to @user any more
      @post   = posts(:default)
      @post.user # equal to @admin above
      current_time # stubbed to be 6/1/2007 using mocha or rspec

  # rspec example
  require 'model_stubbing'

  describe Foo do


* Support other mocking frameworks besides rspec and mocha?

* Support Sequel, DataMapper, etc?  I use AR, but I'm willing to abstract
  some of that out in case someone wants to support another ORM.
Something went wrong with that request. Please try again.