You are here: Browse Railsplugins 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?
class FooTest < Test::Unit::TestCase
define_models do
time 2007, 6, 1
end
model User do
stub :name => 'bob', :admin => false
stub :admin, :admin => true # inherits from default fixture
end
end
model Post do
# uses admin user fixture above
stub :title => 'initial', :user => all_stubs(:admin_user),
:published_at => current_time + 5.days
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
end
@post = posts(:default)
@post.user # equal to @admin above
current_time # stubbed to be 6/1/2007 using mocha or rspec
describe Foo do
...
end
NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly