javaclojurebddspeclj

Clojure Speclj with-redefs and a stub from a different thread


I'm testing a function to make sure that it calls a function that's in another namespace. I'd like to stub the function out (using speclj stub), so I can record the invocations.

(defn fn-under-test []
  (p/helper-fn))

(describe "test"
  (with-stubs)
  (around [it]
    (with-redefs [p/helper-fn (stub :helper)]
      (it)))
  (it "should call the helper-fn"
    (fn-under-test)
    (should-have-invoked :helper {:times 1})))

I get an exception:

java.lang.Exception: Stub recoding not bound.  Please add (with-stubs) to the decribe/context.

If helper-fn is defined in the current namespace, everything works as expected. How can I stub a function in another namespace using speclj?

EDIT: The exception occurs when the stubbed function is called from a different thread. I created a pull request that fixes the issue.


Solution

  • I coded up your example and the specs ran fine; passed without error. I can't think of a reason stubbing fns in a another namespace wouldn't work. I do it all the time.

    Here's a gist showing my work: https://gist.github.com/slagyr/2aed1ccfd8ec702d7051