public
Description: Rails plugin for automatically generating valid model instances for use in tests
Homepage:
Clone URL: git://github.com/jaz303/create_valid.git
name age message
file README Loading commit data...
file Rakefile
file init.rb
file install.rb
directory lib/
directory tasks/
directory test/
file uninstall.rb
README
CreateValid
===========

Adds method_missing hooks to Test::Unit::TestCase which automatically
build/create valid model instances for use in tests.

The supported hooks are:

(build|create)(_valid)?(_underscored_model_name)
augment_underscored_model_name
params_for_underscored_model_name

Sequence Generation
===================

You can get a unique integer by calling next_sequence. This is useful for
creating valid models that have uniqueness constraints, e.g.

def params_for_user
  { :email => "jason#{next_sequence}@magiclamp.co.uk" }
end

Test::Unit
==========





The hook is of the form 'create_valid_{underscored_model_name}'. Calling such
a method will create an instance of the model and walk over the class hierarchy,
starting at the first subclass of ActiveRecord::Base and ending at the model
class itself. At each step, 'augment_{class}' is called, where 'class'
is the underscored name of the current class in the hierarchy. If no
implementation is provided, we search for a method 'params_for_{class}',
which is expected to return a hash to be merged with the model's attributes. If
this method doesn't exist, no action is taken.

The next_sequence method is used to generate a unique integer - useful for models
with uniqueness constraints.

An example:

class Person < ActiveRecord::Base
  validates_presence_of :forename, :surname
  validates_uniqueness_of :email
end

class Manager < Person
  validates_presence_of :department
end

class MyTest < Test::Unit::TestCase
  include CreateValid

  def params_for_person
    { :forename => 'Rob', :surname => 'Roy', :email => "test#{next_sequence}@test.com" }
  end
  
  def augment_manager(m)
    m.department = 'Widgets'
  end
    
  def setup
    @manager = create_valid_manager
  end

end