Coder Social home page Coder Social logo

duck_spy's Introduction

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.

spy = DuckSpy.new

spy.foo(42)

spy.calls

Returns:

{
    :foo => {
               :args => [
            42
        ],
        :result_duck => {}
    }
}

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

spy = DuckSpy.new

spy.foo.baz(:some, 'args')

spy.calls

Returns:

{
    :foo => {
               :args => [],
        :result_duck => {
            :baz => {
                       :args => [
                    :some,
                    "args"
                ],
                :result_duck => {}
            }
        }
    }
}

Stubbing

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

spy = DuckSpy.new

spy.behave_like(a: 35, b: 7)

spy.a + spy.b  # => 42

Or, lazily:

spy = DuckSpy.new

spy.stub(:add) { |a, b| a + b }

spy.add(35, 7) # => 42

duck_spy's People

Contributors

mjgpy3 avatar

Watchers

 avatar

duck_spy's Issues

Reset functionality

It would be nice to have a #reset! method on a duck spy, so that in irb sessions the same ds can be used without creating new instance.

Add ability to stub results

"Stubbed result" functionality would be nice, e.g.

spy = DuckSpy.new
spy.behave_like(foo: 42)
spy.foo # => 42

The foo call should still be saved as a call. Also, block definitions (e.g. laziness) should be implemented:

spy = DuckSpy.new
spy.stub(:foo) { 42 }
spy.foo # => 42

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.