项目作者: mjgpy3

项目描述 :
Discovers ruby interfaces
高级语言: Ruby
项目地址: git://github.com/mjgpy3/duck_spy.git
创建时间: 2014-12-04T04:08:04Z
项目社区:https://github.com/mjgpy3/duck_spy

开源协议:

下载


duck_spy

Build Status

A spy that can be passed to ruby code, and describe what kind of interfaces it expects.

Motivation

In languages that use a fair amount of duck-typing (e.g. Ruby), it’s nice to know exactly what duck a method expects (i.e. it’s implicit interface).

The hope is that the “duck spy” will touch certain paths of code to, at least, make these interfaces somewhat more evident.

Examples

Spy behavior

A DuckSpy tells you what methods (and what arguments) have been called.

  1. spy = DuckSpy.new
  2. spy.foo(42)
  3. spy.calls

Returns:

  1. {
  2. :foo => {
  3. :args => [
  4. 42
  5. ],
  6. :result_duck => {}
  7. }
  8. }

A DuckSpy can also tell you what interface it expects its result to be able to fulfil.

  1. spy = DuckSpy.new
  2. spy.foo.baz(:some, 'args')
  3. spy.calls

Returns:

  1. {
  2. :foo => {
  3. :args => [],
  4. :result_duck => {
  5. :baz => {
  6. :args => [
  7. :some,
  8. "args"
  9. ],
  10. :result_duck => {}
  11. }
  12. }
  13. }
  14. }

Stubbing

A DuckSpy‘s behavior can be stubbed one of two ways:

  1. spy = DuckSpy.new
  2. spy.behave_like(a: 35, b: 7)
  3. spy.a + spy.b # => 42

Or, lazily:

  1. spy = DuckSpy.new
  2. spy.stub(:add) { |a, b| a + b }
  3. spy.add(35, 7) # => 42