Class: ModelMocker
- Inherits:
-
Object
- Object
- ModelMocker
- Defined in:
- lib/model_mocker.rb,
lib/model_mocker/railtie.rb
Overview
An easy way of generating partially mocked ActiveRecord model objects. This is a useful way of simulating some aspects of a model (like the persistence layer) but leaving the domain logic parts intact, so that you can test those in isolation, without worrying about database reads and writes.
Defined Under Namespace
Modules: ActiveRecordHook, Helpers Classes: Railtie
Instance Method Summary collapse
-
#as_new_record ⇒ Object
The model instance will always report that it’s a new record.
-
#can_be_destroyed ⇒ Object
The model instance will return true if
#destroyis called. -
#cannot_be_destroyed ⇒ Object
The model instance will return false if
#destroyis called. -
#cannot_be_updated ⇒ Object
The model instance cannot be updated:
#update_attributeswill always return false. -
#initialize(klass, stub_params = {}) ⇒ ModelMocker
constructor
ModelMocker.new is called with the AR::Base subclass a mock object is for, with any creation params in stub_params.
-
#instance ⇒ Object
:nodoc:.
-
#invalid ⇒ Object
Validity-related methods will always report that the instance is invalid,
#savewill return false, and#save!will raiseActiveRecord::RecordNotSaved. -
#valid ⇒ Object
Validity-related methods will always report that the instance is valid, and
#saveand#save!will return true (without actually saving anything).
Constructor Details
#initialize(klass, stub_params = {}) ⇒ ModelMocker
ModelMocker.new is called with the AR::Base subclass a mock object is for, with any creation params in stub_params. Methods on the ModelMocker instance determine how the mock AR::Base subclass object will behave
47 48 49 50 |
# File 'lib/model_mocker.rb', line 47 def initialize(klass, stub_params = {}) @klass = klass @stub_params = stub_params end |
Instance Method Details
#as_new_record ⇒ Object
The model instance will always report that it’s a new record
66 67 68 |
# File 'lib/model_mocker.rb', line 66 def as_new_record instance.stubs(:new_record?).returns(true) end |
#can_be_destroyed ⇒ Object
The model instance will return true if #destroy is called
92 93 94 |
# File 'lib/model_mocker.rb', line 92 def can_be_destroyed instance.stubs(:destroy).returns(true) end |
#cannot_be_destroyed ⇒ Object
The model instance will return false if #destroy is called
97 98 99 |
# File 'lib/model_mocker.rb', line 97 def cannot_be_destroyed instance.stubs(:destroy).returns(false) end |
#cannot_be_updated ⇒ Object
The model instance cannot be updated: #update_attributes will always return false
87 88 89 |
# File 'lib/model_mocker.rb', line 87 def cannot_be_updated instance.stubs(:update_attributes).returns(false) end |
#instance ⇒ Object
:nodoc:
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/model_mocker.rb', line 52 def instance # :nodoc: return @instance unless @instance.nil? id = @stub_params[:id] @instance = @klass.new attributes.each do |k, v| @instance.send(:write_attribute, k, v) end @instance.stubs(:id).returns(id) stub_instance_methods! @instance.extend(ModelMocker::Helpers) @instance end |
#invalid ⇒ Object
Validity-related methods will always report that the instance is invalid, #save will return false, and #save! will raise ActiveRecord::RecordNotSaved
80 81 82 83 84 |
# File 'lib/model_mocker.rb', line 80 def invalid instance.stubs(:valid?).returns(false) instance.stubs(:save).returns(false) instance.stubs(:save!).raises(ActiveRecord::RecordNotSaved) end |
#valid ⇒ Object
Validity-related methods will always report that the instance is valid, and #save and #save! will return true (without actually saving anything)
72 73 74 75 76 |
# File 'lib/model_mocker.rb', line 72 def valid instance.stubs(:valid?).returns(true) instance.stubs(:save).returns(true) instance.stubs(:save!).returns(true) end |