Git Product home page Git Product logo

amok's Introduction

= Amok, a compact mock library

Amok is a simple and compact mock library that is independent of any
testing framework but made to work nicely with test/spec and bacon.
It provides mocking and stubbing of any object's methods.


== Whirl-wind tour

  Amok.new(Item) {
    on.find(1) {
      Amok.with(:name => "Deschutes",
                :description => "Deschutes model Guitar",
                :unit_price => Money.new(2400.00))
    }
  }

  Item.find(1).name.should.equal "Deschutes"

  Amok.with(Google4R::Checkout::CheckoutCommand) { |obj, mock|
    mock.on.new { |*args|
      Amok.new(Google4R::Checkout::CheckoutCommand.new(*args)) {
        need(1).send_to_google_checkout {
          Amok.with(:redirect_url => "http://google.response.url")
        }
      }
    }

    Item.find(1).purchase.redirect_url.should.equal "http://google.response.url"
  }


== Implemented features

Amok.new(obj)::
  creates a mock definition object to stub methods of obj.

Amok.with(obj) { |obj, mock| ... }::
  Use Amok.with to automatically check at the end of the block whether
  required methods have been called.

Amok.make(hash)::
  is used to conveniently make mocks that return the hash values when
  the hash key is sent.

mock.on._method_ {...}::
  run the block when _method_ is sent.  The block can call
  +mock.previous(_method_, ...)+ to call the original method.

mock.on._method_(_args_...) {...}::
  run the block when _method_ is sent with exactly the same _args_,
  else run the original method.  The block can call
  +mock.previous(_method_, ...)+ to call the original method.

mock.on(method, args) {...}::
  like mock.on._method_, but _method_ can be passed as a symbol and _args_.
  (This allows you to define methods that require empty _args_,
  because mock.on._method_() does not check _args_.)

mock.need._method_::
  check that _method_ is called

mock.need(n)._method_::
  check that _method_ is called exactly _n_ times.

mock.need._method_ {...}::
  like mock.on._method_ {...}, but check that _method_ is called.

mock.need(n)._method_ {...}::
  like mock.on._method_ {...}, but check that _method_ is called exactly
  _n_ times.

mock.need(method, args, n) {...}::
  same as mock.need(n)._method_(args).

mock.never._method_::
  check that _method_ is never called.

mock.never(method)::
  same as mock.never._method_.

mock.successful?::
  returns true if all checks passed.

mock.errors::
  returns an array of failed checks.

mock.validate::
  raises Amok::Failed when checks failed.


== Words of advice

Mocking and stubbing are possibly dangerous operations, and easily can
change the meaning of your tests in ways you don't intent.  Be alert,
and only use them when needed.  Remember, the purpose of your tests is
to test your code, and not test your mocks.


== Thanks to

* raggi and jazen for making me write it.
* mfp for instance_exec.
* authors of all previous mock libraries for inspiration.


== History

* September 25th, 2008: Coding started.


== Contact

Please mail bugs, suggestions and patches to
<mailto:[email protected]>.

Git repository (rebased patches on master are most welcome):
git://github.com/chneukirchen/amok.git

Project page:
http://github.com/chneukirchen/amok/tree/master


== Copying

Copyright (C) 2008 Christian Neukirchen <http://purl.org/net/chneukirchen>

Amok is freely distributable under the terms of an MIT-style license.
See COPYING or http://www.opensource.org/licenses/mit-license.php.


== Links

Mocks Aren't Stubs:: <http://martinfowler.com/articles/mocksArentStubs.html>

Christian Neukirchen:: <http://chneukirchen.org/>

amok's People

Contributors

leahneukirchen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

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.