项目作者: metametadata

项目描述 :
An isolation framework for Clojure/ClojureScript.
高级语言: Clojure
项目地址: git://github.com/metametadata/clj-fakes.git
创建时间: 2015-09-18T16:07:07Z
项目社区:https://github.com/metametadata/clj-fakes

开源协议:MIT License

下载


clj-fakes is an isolation framework for Clojure/ClojureScript that makes creating test doubles much easier.

Clojars Project
Gitter

Features

  • All test doubles are named “fakes” to simplify the terminology.
  • Fakes can be created for:
    • functions
    • instances of protocols and Java interfaces
  • “Nice” and “strict” protocol fakes are supported.
  • Monkey patching is supported to fake implicit dependencies.
  • Several functions are provided for asserting recorded calls.
  • Self-testing: automatically checks for unused fakes.
  • Informative error messages.
  • Test runner agnostic.
  • Arrange-Act-Assert style testing.

Installation

Requirements: Clojure 1.7.0+ and/or ClojureScript 1.10.238+.

Add this to your dependencies:

  1. [clj-fakes "0.12.0"]

Require framework namespace in your unit test source file:

  1. (ns unit.example
  2. (:require
  3. [clj-fakes.core :as f]
  4. ; and/or:
  5. [clj-fakes.context :as fc]))

Cheat Sheet

Creating Faking Context

Explicit context:

  1. (let [ctx (fc/context)]
  2. ; use clj-fakes.context API here
  3. )

Implicit context:

  1. (f/with-fakes
  2. ; use clj-fakes.core API here
  3. )
  4. ; on exit block will automatically unpatch all patched vars and execute self-tests

All the following examples are assumed to be used inside an implicit context.

Stubbing

Function Stub

  1. (let [foo (f/fake [[1 2] "foo"
  2. [3 4 5] "bar"])]
  3. (foo 1 2) ; => "foo"
  4. (foo 3 4 5) ; => "bar"
  5. (foo 100 200)) ; => raises "Unexpected args are passed into fake: (100 200) ..."

Method Stub

  1. (let [cow (f/reify-fake p/AnimalProtocol
  2. (sleep :fake [[] "zzz"]))]
  3. (p/sleep cow) ; => "zzz"
  4. (p/speak cow)) ; => undefined method exception

Nice Method Stub

  1. (let [cow (f/reify-nice-fake p/AnimalProtocol)]
  2. (p/sleep cow) ; => FakeReturnValue
  3. (p/speak cow)) ; => FakeReturnValue

Mocking

Function Mock

  1. (let [foo (f/recorded-fake [[(f/arg integer?) (f/arg integer?)] #(+ %1 %2)])
  2. bar (f/recorded-fake [[(f/arg integer?) (f/arg integer?)] #(* %1 %2)])]
  3. (foo 1 2)
  4. (bar 5 6)
  5. (foo 7 8)
  6. (f/calls foo)
  7. ; => [{:args [1 2] :return-value 3}
  8. ; {:args [7 8] :return-value 15}]
  9. (f/calls)
  10. ; => [[foo {:args [1 2] :return-value 3}]
  11. ; [bar {:args [5 6] :return-value 30}]
  12. ; [foo {:args [7 8] :return-value 15}]]
  13. )

Method Mock

  1. (let [cow (f/reify-fake p/AnimalProtocol
  2. (speak :recorded-fake [f/any "moo"]))]
  3. (p/speak cow)
  4. (f/calls (f/method cow p/speak))) ; => [{:args ..., :return-value moo}]

Assertions

These functions return true or raise an exception and can be used only with recorded fakes.

Strictly One Call

  1. (f/was-called-once foo [1 2])
  2. (f/method-was-called-once p/speak cow ["Bob"])

At Least One Call

  1. (f/was-called foo [1 2])
  2. (f/method-was-called p/speak cow ["Bob"])

Strictly One Call Matched The Provided Args Matcher

  1. (f/was-matched-once foo [1 2])
  2. (f/method-was-matched-once p/speak cow ["Bob"])

No Calls

  1. (f/was-not-called foo)
  2. (f/method-was-not-called p/speak cow)

Calls In The Specified Order

  1. (f/were-called-in-order
  2. foo [1 2 3]
  3. foo [(f/arg integer?)]
  4. bar [100 200]
  5. baz [300])
  6. (f/methods-were-called-in-order
  7. p/speak cow []
  8. p/sleep cow []
  9. p/eat dog ["dog food" "water"]
  10. p/speak cow ["Bob"])

Monkey Patching

Caution: this feature is not thread-safe.
Strongly consider avoiding it in Clojure code if you plan to someday run your tests concurrently.

Patch Function With Stub

  1. (f/with-fakes
  2. (f/patch! #'funcs/sum (f/fake [[1 2] "foo"
  3. [3 4] "bar"]))
  4. (funcs/sum 1 2) ; => "foo"
  5. (funcs/sum 3 4)) ; => "bar"
  6. ; patching is reverted on exiting with-fakes block
  7. (funcs/sum 1 2) ; => 3

Patch To Spy

  1. (f/patch! #'funcs/sum (f/recorded-fake [f/any funcs/sum]))
  2. (funcs/sum 1 2) ; => 3
  3. (f/was-called funcs/sum [1 2]) ; => true

Self-tests

  1. (f/with-fakes
  2. (f/fake [f/any nil]))
  3. ; => raises "Self-test: no call detected for: non-optional fake ..."
  4. (f/with-fakes
  5. (f/recorded-fake))
  6. ; => raises "Self-test: no check performed on: recorded fake ..."

Documentation

More documentation can be found at the project site:

References

The API was mainly inspired by jMock and
unittest.mock frameworks with
design decisions loosely based on the
“Fifteen things I look for in an Isolation framework” by Roy Osherove.

Some alternative frameworks with isolation capabilities:

Also take at look at the article
“Isolating External Dependencies in Clojure” by Joseph Wilk.

For more detailed information about unit testing, TDD and test double patterns I’d recommend the books below:

  • “Test Driven Development: By Example” by Kent Beck
  • “Growing Object-Oriented Software, Guided by Tests” by Steve Freeman and Nat Pryce [site]
  • “xUnit Test Patterns: Refactoring Test Code” by Gerard Meszaros [site]

License

Copyright © 2015 Yuri Govorushchenko.

Released under an MIT license.