Class: Quarry::Mock::Object

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/quarry/mock/object.rb

Overview

Mock Object

A straightfoward mocking facility. Typically used in test cases. The Mock class offers a few constructors for quickly building mockups.

mock - Returns a static reponse.
echo - Returns the arguments passed-in.
spin - Returns a rotation of responses.
keys - Returns an index of responses.

Mock classes can be built from sratch or partially framed against other classes.

Usage

class ContextMock < Mock
  mock :response_headers, {}
  spin :host_url, ['http://www.nitrohq.com','http://www.rubyforge.com']
end

ctx = ContextMock.new
ctx.response_headers['location'] = url
ctx.host_url   #=> "http://www.nitrohq.com"
ctx.host_url   #=> "http://www.rubyforge.com"

Or

class ContextMock < Mock(Context)
  ...
end

Constant Summary collapse

UnmockedMethods =

Certain methods are not mocked:

inspect       (tricky)
class         (delegated)
kind_of?      (delegated)
is_a?         (delegated)
instance_of?  (delegated)
method        (works as-is)
send          (works as-is)
respond_to?   (works as-is)
hash          (no way to mock)

__id__, __call__, etc. (not meant to be mocked, ever!)
%r{^(
  |inspect
  |kind_of\?|is_a\?|instance_of\?|class
  |method|send|respond_to\?
  |hash
  |__
)}x

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.mocked_classObject (readonly)

Returns the value of attribute mocked_class.



91
92
93
# File 'lib/quarry/mock/object.rb', line 91

def mocked_class
  @mocked_class
end

Class Method Details

.echo(sym) ⇒ Object

Responds with input.



103
104
105
# File 'lib/quarry/mock/object.rb', line 103

def echo( sym )
  define_method( sym ) { |*args| args }
end

.keys(sym, hsh) ⇒ Object

Responds according to a mapping of input parameters.



113
114
115
# File 'lib/quarry/mock/object.rb', line 113

def keys( sym, hsh )
  define_method( sym ) { |*args| hsh[args] }
end

.mock(sym, val) ⇒ Object

Mock a static repsonse.



98
99
100
# File 'lib/quarry/mock/object.rb', line 98

def mock( sym, val )
  define_method( sym ) { |*args| val }
end

.mocksObject



93
94
95
# File 'lib/quarry/mock/object.rb', line 93

def mocks
  self.methods(false)
end

.spin(sym, arr) ⇒ Object

Reponds with a rotation of reponses.



108
109
110
# File 'lib/quarry/mock/object.rb', line 108

def spin( sym, arr )
  define_method( sym ) { |*args| arr.push(arr.shift) ; arr[-1] }
end

Instance Method Details

#__classObject

Delegate methods: #class, instance_of?, kind_of?, and is_a?



121
# File 'lib/quarry/mock/object.rb', line 121

alias :__class :class

#classObject

:nodoc:



123
124
125
# File 'lib/quarry/mock/object.rb', line 123

def class # :nodoc:
  return __class.mocked_class
end

#instance_of?(klass) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


127
128
129
# File 'lib/quarry/mock/object.rb', line 127

def instance_of?( klass ) # :nodoc:
  self.class == klass
end

#kind_of?(klass) ⇒ Boolean Also known as: is_a?

:nodoc:

Returns:

  • (Boolean)


131
132
133
# File 'lib/quarry/mock/object.rb', line 131

def kind_of?( klass ) # :nodoc:
  self.class <= klass
end