Skip to main content

mock

Jump to function (19)

mock/call-count#

(call-count mock-fn)

Returns the number of times the mock was called.

Example:

(def my-mock (mock :result))

mock/called-once?#

(called-once? mock-fn)

Returns true if the mock was called exactly once.

Example:

(def my-mock (mock :result))

mock/called-times?#

(called-times? mock-fn n)

Returns true if the mock was called exactly n times.

Example:

(def my-mock (mock :result))

mock/called-with?#

(called-with? mock-fn & expected-args)

Returns true if the mock was called with the exact arguments.

Example:

(def my-mock (mock :result))

mock/called?#

(called? mock-fn)

Returns true if the mock was called at least once.

Example:

(def my-mock (mock :result))

mock/calls#

(calls mock-fn)

Returns a list of all argument lists the mock was called with.

Example:

(def my-mock (mock :result))

mock/clear-all-mocks!#

(clear-all-mocks!)

Clears the entire mock registry. Useful for cleanup between test suites in long-running processes.

Example:

(clear-all-mocks!) ; All mocks removed from registry

mock/first-call#

(first-call mock-fn)

Returns the arguments from the first call.

Example:

(def my-mock (mock :result))

mock/last-call#

(last-call mock-fn)

Returns the arguments from the most recent call.

Example:

(def my-mock (mock :result))

mock/mock#

(mock return-value)

Creates a mock function that returns a fixed value and tracks all calls.

Example:

(def my-mock (mock :return-value))

mock/mock-fn#

(mock-fn f)

Creates a mock function with custom behavior that tracks all calls.

Example:

(def my-mock (mock-fn (fn [x] (* x 2))))

mock/mock-returning#

(mock-returning values)

Creates a mock that returns different values for consecutive calls. After exhausting values, returns the last value.

Example:

(def my-mock (mock-returning [1 2 3]))

mock/mock-throwing#

(mock-throwing exception)

Creates a mock that throws an exception when called.

Example:

(def my-mock (mock-throwing (php/new \RuntimeException "API unavailable")))

mock/mock?#

(mock? f)

Returns true if the function is a mock.

mock/never-called?#

(never-called? mock-fn)

Returns true if the mock was never called.

Example:

(def my-mock (mock :result))

mock/reset-mock!#

(reset-mock! mock-fn)

Resets the call history of a mock without removing it from the registry. The mock can continue to be used and track new calls.

Example:

(def my-mock (mock :result))

mock/spy#

(spy f)

Wraps an existing function to track calls while preserving original behavior.

Example:

(def original-fn (fn [x] (* x 2)))

mock/with-mock-wrapper#

(with-mock-wrapper bindings & body)

Like with-mocks but for wrapped mocks (interop scenarios). Automatically resets the underlying mock even when wrapped in an adapter function.

Usage:

(with-mock-wrapper [fn-symbol underlying-mock wrapper-fn]
body...)

Multiple wrappers:

(with-mock-wrapper [service-a mock-a (fn [x] (mock-a (inc x)))
service-b mock-b (fn [y] (mock-b (dec y)))]
...)

Example:

(with-mock-wrapper [http mock-http identity] (http "test"))

mock/with-mocks#

(with-mocks bindings & body)

Temporarily replaces functions with mocks using with-redefs. Automatically resets mocks after the body executes.

Works with inline mock creation:

(with-mocks [http-get (mock {:status 200})]
(http-get)
;; Mock is automatically reset after this block)

Also works with pre-defined mocks:

(let [my-mock (mock :result)]
(with-mocks [some-fn my-mock]
(some-fn)))

If you need to wrap the mock in a function (e.g., to adapt arguments), you'll need to manually reset:

(with-mocks [some-fn (fn [& args] (my-mock (transform args)))]
(some-fn)
(reset-mock! my-mock))