Search code examples
clojurestubmidje

midje does not stub functions provided with hashmaps


Consider the following code

(use 'midje.sweet)                                                                                                                                                                                                                                                              
(defn x2 [x] (* x x))                                                                                                                                                                                                                                                           
(def fs {:x2 x2})                                                                                                                                                                                                                                                               
(fact                                                                                                                                                                                                                                                                           
  (x2 1) => "one"                                                                                                                                                                                                                                                               
  ((:x2 fs) 1) => "one"                                                                                                                                                                                                                                                         
  (against-background                                                                                                                                                                                                                                                           
    (#'tweetfetcher.core-test/x2 1) => "one"))    

which outputs

FAIL at (core_test.clj:177)
    Expected: "one"
      Actual: 1
FAILURE: 1 check failed.  (But 32 succeeded.)

The first check is stubbed while the second use x2 as provided by the hashmap fs.

Considering I rule out referencing, why is (:x2 fs) not stubbed ?

Thanks for insights.


Solution

  • I'm not surprised it works this way. In (x2 1), it is known compile-time that x2 is the function defined as (defn x2 [x] (* x x)).

    In ((:x2 fs) 1), we know, compile-time, that fs is {:x2 x2}, but we don't yet know the result of (:x2 fs). By that I mean the expression (:x2 fs) is not evaluated during fact expansion. It probably sees that (:x2 fs) is not a var that resolves to a function and therefore does nothing to associate it with our stub (inside against-background).