Module Spec::Rails::Mocks

  1. lib/spec/rails/mocks.rb


public instance

  1. mock_model
  2. stub_model

Classes and Modules

Module Spec::Rails::Mocks::ModelStubber

Public instance methods

mock_model (model_class, options_and_stubs = {}) {|m if block_given?| ...}

Creates a mock object instance for a model_class with common methods stubbed out. Additional methods may be easily stubbed (via add_stubs) if stubs is passed.

[show source]
# File lib/spec/rails/mocks.rb, line 11
      def mock_model(model_class, options_and_stubs = {})
        id = options_and_stubs[:id] || next_id
        options_and_stubs = options_and_stubs.reverse_merge({
          :id => id,
          :to_param => id.to_s,
          :new_record? => false,
          :errors => stub("errors", :count => 0)
        m = mock("#{}_#{id}", options_and_stubs)
        m.__send__(:__mock_proxy).instance_eval "def @target.as_new_record\nself.stub!(:id).and_return nil\nself.stub!(:to_param).and_return nil\nself.stub!(:new_record?).and_return true\nself\nend\ndef @target.is_a?(other)\n\#{model_class}.ancestors.include?(other)\nend\ndef @target.kind_of?(other)\n\#{model_class}.ancestors.include?(other)\nend\ndef @target.instance_of?(other)\nother == \#{model_class}\nend\ndef @target.class\n\#{model_class}\nend\n"
        yield m if block_given?
stub_model(Model, hash_of_stubs)
stub_model(Model, instance_variable_name, hash_of_stubs)

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

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

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

Database Independence

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


stub_model(Person, :id => 37)
stub_model(Person) do |person|
  person.first_name = "David"
[show source]
# File lib/spec/rails/mocks.rb, line 98
      def stub_model(model_class, stubs={})
        stubs = {:id => next_id}.merge(stubs)
        returning do |model|
 = stubs.delete(:id)
          model.extend ModelStubber
          stubs.each do |k,v|
            if model.has_attribute?(k)
              model[k] = stubs.delete(k)
          yield model if block_given?