GithubHelp home page GithubHelp logo

methodmissing / model_stubbing Goto Github PK

View Code? Open in Web Editor NEW

This project forked from technoweenie/model_stubbing

6.0 4.0 0.0 164 KB

Replacement for ActiveRecord fixtures using an extremely flexible ruby-based approach.

License: MIT License

Ruby 100.00%

model_stubbing's Introduction

model_stubbing
==============

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 (http://errtheblog.com/post/7708)
unit_record (http://www.dcmanges.com/blog/rails-unit-record-test-without-the-database)
Rspec and #mock_model

Exemplar (http://www.bofh.org.uk/articles/2007/08/05/doing-the-fixture-thing)
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 
(http://www.martinfowler.com/bliki/ObjectMother.html) 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'
      end
    
      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)]
      end
    
      model Post do
        # uses admin user fixture above
        stub :title => 'initial', :user => all_stubs(:admin_user), 
          :published_at => current_time + 5.days
      end
    end
    
    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
    end
  end

  # rspec example
  require 'model_stubbing'

  describe Foo do
    ...
  end

TODO
====

* 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.

model_stubbing's People

Contributors

lawrencepit avatar mattly avatar methodmissing avatar technoweenie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.