I've been playing with Amit Rathore's simple mocking functions:
http://s-expressions.com/2010/01/24/conjure-simple-mocking-and-stubbing-for-clojure-unit-tests/

I'm seeing the weirdest effect, and after banging my head on it for a couple
hours I figure it's time to ask about it.  Any ideas would be greatly
appreciated.  I'm still in the process of learning Clojure, and that means
every new thing can be painful to work through the kinks.

*I'm testing this function:*

(defn distances [origin & locations]
  (map (fn [dest] (dist-in-miles origin dest)) locations))

*With this spec:*

(defspec distances-in-miles-to-multiple-locations-from-origin
  (stubbing [dist-in-miles 2.0]
    (distances "Boston,MA" "Albany,NY" "LosAngeles,CA"))
  => [2.0 2.0])

*Which uses this macro wrapper around clojure.test:*

(defmacro defspec [name actual arrow expected]
  `(deftest ~name (is (= ~expected ~actual))))

It seems the stubbing is not happening when (distances "Boston,MA"
"Albany,NY" "LosAngeles,CA") is being evaluated.  But if I put print
statements in the function to see the value of dist-in-miles, it is clearly
2.0!  I have two other specs that use stubbing and work as expected.

*For reference:*

(defspec distances-map-to-multiple-locations-from-origin
  (stubbing [dist-in-miles 2.0]
    (map-of-distances "Boston,MA" "Newport,RI" "LosAngeles,CA"))
  => {"LosAngeles,CA" 2.0, "Newport,RI" 2.0})

(defspec
relative-values-of-locations-dependent-on-frequency-of-visits-per-year
  (stubbing [dist-in-miles 365.0]
    (relative-distances "Boston,MA" "Newport,RI" 2 "LosAngeles,CA" 1))
  => {"LosAngeles,CA" 365.0, "Newport,RI" 730.0})

*Two functions for the above two, working specs:*

(defn map-of-distances [origin & locations]
  (apply hash-map (interleave locations (apply distances origin
locations))))

(defn relative-distances
  "Gives distance * frequency.
  frequencies are in days out of 365"
  [origin & locations-n-frequencies]
  (let [loc-w-dists (apply map-of-distances origin (take-nth 2
locations-n-frequencies))
        loc-w-freqs (apply hash-map locations-n-frequencies)]
    (fmap * loc-w-dists loc-w-freqs)))

Thank in advance,
Alex

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to