module RSpec::Rails::Mocks

def stub_model(model_class, stubs={})

end
person.first_name = "David"
stub_model(Person) do |person|
stub_model(Person, :id => 37)
stub_model(Person).as_new_record
stub_model(Person)

== Examples

a look at libraries like unit_record or NullDB.
from the object itself. To completely decouple from the database, take
loading up its columns from the database. It just prevents data access
database-independent. It does not stop the model class itself from
+stub_model+ does not make your examples entirely

== Database Independence

inherently more state-based than interaction-based.
helper), it is especially useful in view examples, which are
While you can use stub_model in any example (model, view, controller,

example a bit more descriptive.
case new_record? will return true, but using +as_new_record+ makes the
set the id to nil. You can also explicitly set :id => nil, in which
the object to behave as a new record, sending it +as_new_record+ will
This means that by default new_record? will return false. If you want
new_record? is overridden to return the result of id.nil?

mocking/stubbing framework.
key/value pair is assigned as a stub return value using RSpec's
submitted values. If the model does not have a matching attribute, the
matching attribute (determined by asking it) are simply assigned the
database*. For each key in +hash_of_stubs+, if the model has a
Creates an instance of +Model+ that is prohibited from accessing the

stub_model(Model, instance_variable_name, hash_of_stubs)
stub_model(Model, hash_of_stubs)
stub_model(Model).as_new_record
stub_model(Model)
:call-seq:
def stub_model(model_class, stubs={})
  primary_key = model_class.primary_key.to_sym
  stubs = {primary_key => next_id}.merge(stubs)
  model_class.new.tap do |m|
    m.__send__("#{primary_key}=", stubs.delete(primary_key))
    m.extend ModelStubber
    m.stub(stubs)
    yield m if block_given?
  end
end