Module: Micronaut::Rails::Mocking::WithMocha
- Defined in:
- lib/micronaut/rails/mocking/with_mocha.rb
Defined Under Namespace
Modules: ModelStubber
Instance Method Summary collapse
-
#add_stubs(object, params) ⇒ Object
Stubs methods on
object(ifobjectis a symbol or string a new mock with that name will be created). - #mock_model(model_class, params = {}) {|model| ... } ⇒ Object
- #stub_model(model_class, params = {}) {|model| ... } ⇒ Object
Instance Method Details
#add_stubs(object, params) ⇒ Object
Stubs methods on object (if object is a symbol or string a new mock with that name will be created). stubs is a Hash of method=>value
40 41 42 43 44 |
# File 'lib/micronaut/rails/mocking/with_mocha.rb', line 40 def add_stubs(object, params) # :nodoc: m = [String, Symbol].include?(object.class) ? mock(object.to_s) : object params.each { |prop, value| m.stubs(prop).returns(value) } m end |
#mock_model(model_class, params = {}) {|model| ... } ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/micronaut/rails/mocking/with_mocha.rb', line 46 def mock_model(model_class, params = {}) id = params[:id] || next_id model = stub("#{model_class.name}_#{id}", { :id => id, :to_param => id.to_s, :new_record? => false, :errors => stub("errors", :count => 0) }.update(params)) model.instance_eval <<-CODE def as_new_record self.stubs(:id).returns(nil) self.stubs(:to_param).returns(nil) self.stubs(:new_record?).returns(true) self end def is_a?(other) #{model_class}.ancestors.include?(other) end def kind_of?(other) #{model_class}.ancestors.include?(other) end def instance_of?(other) other == #{model_class} end def class #{model_class} end CODE yield model if block_given? return model end |
#stub_model(model_class, params = {}) {|model| ... } ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/micronaut/rails/mocking/with_mocha.rb', line 23 def stub_model(model_class, params = {}) params = params.dup model = model_class.new model.id = params.delete(:id) || next_id model.extend ModelStubber params.keys.each do |prop| model[prop] = params.delete(prop) if model.has_attribute?(prop) end add_stubs(model, params) yield model if block_given? model end |