All Projects → metametadata → Clj Fakes

metametadata / Clj Fakes

Licence: mit
An isolation framework for Clojure/ClojureScript.

Programming Languages

clojure
4091 projects

Projects that are alternatives of or similar to Clj Fakes

Pester
Pester is the ubiquitous test and mock framework for PowerShell.
Stars: ✭ 2,620 (+9976.92%)
Mutual labels:  tdd, mocking, assertions
iutest
c++ testing framework
Stars: ✭ 58 (+123.08%)
Mutual labels:  tdd, assertions
Stubmatic
Mock HTTP calls without coding. Designed specially for testing and testers.
Stars: ✭ 118 (+353.85%)
Mutual labels:  stub, fake
TestBox
TestBox is a next generation testing framework for ColdFusion (CFML) that is based on BDD (Behavior Driven Development) for providing a clean obvious syntax for writing tests. It also includes MockBox, our mocking and stubbing framework.
Stars: ✭ 54 (+107.69%)
Mutual labels:  tdd, mocking
Strictly fake
Stub that automatically verifies that stubbed methods exist and the signatures match the original.
Stars: ✭ 18 (-30.77%)
Mutual labels:  fake, stub
better-mock
Forked from Mockjs, Generate random data & Intercept ajax request. Support miniprogram.
Stars: ✭ 140 (+438.46%)
Mutual labels:  fake, mocking
node-muk
Mock object methods and dependencies.
Stars: ✭ 57 (+119.23%)
Mutual labels:  stub, mocking
Mockaco
🐵 HTTP mock server, useful to stub services and simulate dynamic API responses, leveraging ASP.NET Core features, built-in fake data generation and pure C# scripting
Stars: ✭ 213 (+719.23%)
Mutual labels:  fake, mocking
Hippolyte
HTTP Stubbing in Swift
Stars: ✭ 109 (+319.23%)
Mutual labels:  stub, mocking
Mockqueryable
Moking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc
Stars: ✭ 281 (+980.77%)
Mutual labels:  tdd, mocking
Wiremock.net
WireMock.Net is a flexible library for stubbing and mocking web HTTP responses using request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Stars: ✭ 408 (+1469.23%)
Mutual labels:  tdd, mocking
jaymock-cli
Mock an API and generate fake JSON test data, right from the terminal.
Stars: ✭ 13 (-50%)
Mutual labels:  fake, mocking
Faker.Portable
C# faked data generation for testing and prototyping purpose.
Stars: ✭ 12 (-53.85%)
Mutual labels:  stub, fake
mock-spy-module-import
JavaScript import/require module testing do's and don'ts with Jest
Stars: ✭ 40 (+53.85%)
Mutual labels:  stub, mocking
dummyhttp
Super simple HTTP server that replies a fixed body with a fixed response code
Stars: ✭ 25 (-3.85%)
Mutual labels:  fake, mocking
stub-server
Stub server for REST APIs
Stars: ✭ 14 (-46.15%)
Mutual labels:  stub, fake
Ohhttpstubs
Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers!
Stars: ✭ 4,831 (+18480.77%)
Mutual labels:  mocking, stub
chai
BDD / TDD assertion framework for node.js and the browser that can be paired with any testing framework.
Stars: ✭ 7,842 (+30061.54%)
Mutual labels:  tdd, assertions
ts-mock-imports
Intuitive mocking library for Typescript class imports
Stars: ✭ 103 (+296.15%)
Mutual labels:  stub, fake
automock
A library for testing classes with auto mocking capabilities using jest-mock-extended
Stars: ✭ 26 (+0%)
Mutual labels:  tdd, mocking

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:

[clj-fakes "0.12.0"]

Require framework namespace in your unit test source file:

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

Cheat Sheet

Creating Faking Context

Explicit context:

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

Implicit context:

(f/with-fakes
  ; use clj-fakes.core API here 
)
; 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

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

Method Stub

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

Nice Method Stub

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

Mocking

Function Mock

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

  (f/calls)
  ; => [[foo {:args [1 2] :return-value 3}]
  ;     [bar {:args [5 6] :return-value 30}]
  ;     [foo {:args [7 8] :return-value 15}]]
)

Method Mock

(let [cow (f/reify-fake p/AnimalProtocol
                        (speak :recorded-fake [f/any "moo"]))]
  (p/speak cow)
    
  (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

(f/was-called-once foo [1 2])

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

At Least One Call

(f/was-called foo [1 2])

(f/method-was-called p/speak cow ["Bob"])

Strictly One Call Matched The Provided Args Matcher

(f/was-matched-once foo [1 2])

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

No Calls

(f/was-not-called foo)

(f/method-was-not-called p/speak cow)

Calls In The Specified Order

(f/were-called-in-order
  foo [1 2 3]
  foo [(f/arg integer?)]
  bar [100 200]
  baz [300])

(f/methods-were-called-in-order
  p/speak cow []
  p/sleep cow []
  p/eat dog ["dog food" "water"]
  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

(f/with-fakes
  (f/patch! #'funcs/sum (f/fake [[1 2] "foo"
                                 [3 4] "bar"]))
  (funcs/sum 1 2) ; => "foo"
  (funcs/sum 3 4)) ; => "bar"

; patching is reverted on exiting with-fakes block
(funcs/sum 1 2) ; => 3

Patch To Spy

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

Self-tests

(f/with-fakes
  (f/fake [f/any nil]))
; => raises "Self-test: no call detected for: non-optional fake ..."

(f/with-fakes
  (f/recorded-fake))
; => 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].