Class | Mock |
In: |
lib/facets/more/mock.rb
|
Parent: | OpenStruct |
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.
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
UnmockedMethods | = | %r{^( |inspect |kind_of\?|is_a\?|instance_of\?|class |method|send|respond_to\? |hash |__ )}x |
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!) |
class | -> | __class |
Delegate methods: class, instance_of?, kind_of?, and is_a? | ||
kind_of? | -> | is_a? |
mocked_class | [R] |