Method: Spec::Rails::Mocks#mock_model
- Defined in:
- lib/spec/rails/mocks.rb
#mock_model(model_class, options_and_stubs = {}) {|m| ... } ⇒ Object
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.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/spec/rails/mocks.rb', line 11 def mock_model(model_class, = {}) id = [:id] || next_id = .reverse_merge({ :id => id, :to_param => id.to_s, :new_record? => false, :errors => stub("errors", :count => 0) }) m = mock("#{model_class.name}_#{id}", ) m.__send__(:__mock_proxy).instance_eval <<-CODE def @target.as_new_record self.stub!(:id).and_return nil self.stub!(:to_param).and_return nil self.stub!(:new_record?).and_return true self end def @target.is_a?(other) #{model_class}.ancestors.include?(other) end def @target.kind_of?(other) #{model_class}.ancestors.include?(other) end def @target.instance_of?(other) other == #{model_class} end def @target.class #{model_class} end CODE yield m if block_given? m end |