Skip to content

Private functions

marick edited this page Mar 19, 2013 · 5 revisions
Clone this wiki locally

Executable examples

One Clojure idiom for reaching functions that have been declared private is to call them via their var, rather than via an interned symbol:

    (scratch.core/super-secret-function 5)   ; doesn't work
    (#'scratch.core/super-secret-function 5) ; works

That works fine inside checkables:

      (#'scratch.core/super-secret-function 1) => 6)


Calling private functions through vars looks ugly, so there's a utility function to make them available in the test namespace:

(use '[midje.util :only [testable-privates]])

(testable-privates scratch.core super-secret-function)

(fact (super-secret-function 4) => 4)

Notice that testable-privates isn't part of Midje's default interface.


Consider that tests—while being clients of the code under test—are a special kind of client, one that can more reasonably be expected to have some (if not total) knowledge of a namespace's internals. We can formalize that distinction by declaring that an otherwise-private function should be available to tests. That's done with metadata:

(defn- ^{:testable true}
  not-so-secret-function [n] ...)

Then, within the test namespace, all such "testables" can be interned:

(use '[midje.util :only [expose-testables]])

(expose-testables scratch.core2)

(fact (not-so-secret-function 4) => 4)
Something went wrong with that request. Please try again.