Class: Mock
Overview
:title: Mock
A straightfoward mocking facility. Typically used in test cases. The Mock class offers a few constructors fro quickly building mockups.
mock - Returns a static reponse.
echo - Returns the arguments passed-in.
spin - Returns a rotation of reponses.
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
Direct Known Subclasses
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
-
.mocked_class ⇒ Object
readonly
Returns the value of attribute mocked_class.
Class Method Summary collapse
-
.echo(sym) ⇒ Object
Responds with input.
-
.keys(sym, hsh) ⇒ Object
Responds according to a mapping of input parameters.
-
.mock(sym, val) ⇒ Object
Mock a static repsonse.
- .mocks ⇒ Object
-
.spin(sym, arr) ⇒ Object
Reponds with a rotation of reponses.
Instance Method Summary collapse
-
#__class ⇒ Object
Delegate methods: #class, instance_of?, kind_of?, and is_a?.
-
#class ⇒ Object
:nodoc:.
-
#instance_of?(klass) ⇒ Boolean
:nodoc:.
-
#kind_of?(klass) ⇒ Boolean
(also: #is_a?)
:nodoc:.
Class Attribute Details
.mocked_class ⇒ Object (readonly)
Returns the value of attribute mocked_class.
87 88 89 |
# File 'lib/mega/mock.rb', line 87 def mocked_class @mocked_class end |
Class Method Details
.echo(sym) ⇒ Object
Responds with input.
99 100 101 |
# File 'lib/mega/mock.rb', line 99 def echo( sym ) define_method( sym ) { |*args| args } end |
.keys(sym, hsh) ⇒ Object
Responds according to a mapping of input parameters.
109 110 111 |
# File 'lib/mega/mock.rb', line 109 def keys( sym, hsh ) define_method( sym ) { |*args| hsh[args] } end |
.mock(sym, val) ⇒ Object
Mock a static repsonse.
94 95 96 |
# File 'lib/mega/mock.rb', line 94 def mock( sym, val ) define_method( sym ) { |*args| val } end |
.mocks ⇒ Object
89 90 91 |
# File 'lib/mega/mock.rb', line 89 def mocks self.methods(false) end |
.spin(sym, arr) ⇒ Object
Reponds with a rotation of reponses.
104 105 106 |
# File 'lib/mega/mock.rb', line 104 def spin( sym, arr ) define_method( sym ) { |*args| arr.push(arr.shift) ; arr[-1] } end |
Instance Method Details
#__class ⇒ Object
Delegate methods: #class, instance_of?, kind_of?, and is_a?
116 |
# File 'lib/mega/mock.rb', line 116 alias :__class :class |
#class ⇒ Object
:nodoc:
117 118 119 |
# File 'lib/mega/mock.rb', line 117 def class # :nodoc: return __class.mocked_class end |
#instance_of?(klass) ⇒ Boolean
:nodoc:
120 121 122 |
# File 'lib/mega/mock.rb', line 120 def instance_of?( klass ) # :nodoc: self.class == klass end |
#kind_of?(klass) ⇒ Boolean Also known as: is_a?
:nodoc:
123 124 125 |
# File 'lib/mega/mock.rb', line 123 def kind_of?( klass ) # :nodoc: self.class <= klass end |