0

I'm testing a function that uses two other functions from other namespace.

(fact "a test"
      (let [result (function-that-uses-functions-from-other-namespace)]
      result => truthy))

I want to stub out the functions from other namespace and I'm having problems writing the test.

(fact "a test"
      (let [result (function-that-uses-functions-from-other-namespace)]
          (function-that-uses-functions-from-other-namespace)
          (provided (other-namespace/function1 "parameter") => "stub_response"))

but this approach don't seem to work. Any hints? Is it simply the case of let being evaluated before checkables as suggested in Using midje provided in a let clause doesn't stub methods

Community
  • 1
  • 1
sumek
  • 26,495
  • 13
  • 56
  • 75
  • Could you update your example? You don't seem to use `result` anywhere in the second example and you seem to invoke the the same function twice. So it's not clear to me what your second code snippet is trying to achieve. In general, this structure cannot work and yes you're exactly right, the problem is covered by the question you link to. – joelittlejohn Apr 09 '14 at 21:57

3 Answers3

4

You could use against-background that will stub out function calls not only for single facts but whole fact forms:

(fact "a test"
      ...
      (against-background
        (other-namespace/function1 "parameter") => "stub_response"))

Actually, that would probably have worked for the question you linked, too.

xsc
  • 5,983
  • 23
  • 30
  • 1
    +1 This is a nice approach in general, it lets you be very clear which of your tests are affected by the stubbing. – Conan May 28 '14 at 12:39
0

Try without the let as it would execute the function immediately, before the prerequisites are set up:

(fact "a test"
  (function-that-uses-functions-from-other-namespace) => truthy
  (provided (some-internal-func arg1 arg2) => some-result
            (other-internal-func) => other-result))

You just need to have the functions to be stubbed required. They can be from another namespace.

Note that protocol methods cannot be stubbed this way.

muhuk
  • 15,777
  • 9
  • 59
  • 98
0

Does it work using (prerequisite)? Something like this:

(fact "a test"
  (prerequisite (other-namespace-function1 "parameter) => "stub_response")
  (function-that-uses-functions-from-other-namespace) => truthy))

That should let you specify return values for any old function you like, which will be replayed when you run the actual fact.

Have a look at fact-wide-prerequisites. I much prefer this to provided, because it lets me stick to a given-when-then style of testing, still using the same syntax as the actual checkers.

Ondrej Slinták
  • 31,386
  • 20
  • 94
  • 126
Conan
  • 2,288
  • 1
  • 28
  • 42