Class Mock
In: lib/facets/more/mock.rb
Parent: OpenStruct

Mock

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

Methods

echo   keys   mock   mocks   spin  

Constants

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!)

External Aliases

class -> __class
  Delegate methods: class, instance_of?, kind_of?, and is_a?
kind_of? -> is_a?

Attributes

mocked_class  [R] 

Public Class methods

Responds with input.

Responds according to a mapping of input parameters.

Mock a static repsonse.

Reponds with a rotation of reponses.

[Validate]