mercredi 1 juillet 2015

clojure unit-testing with-redefs

I have something like this:

(ns server.core
  (:require [db.api :as d]))

(defrecord Server [host port instance]
  (start [c]
    (let [connection (d/connect (:host c) (:port c))]
      (assoc c :instance connection)))
  (stop [c]
    ;; close the connection
    ))

(defn new-server
  [host port]
  (map->Server {:host host
                :port port}))

And the unit-tests code

(ns server.core_test
  (:require [server.core :refer :all]
            [clojure.test :refer :all]))

(deftest server-test
  (testing "Calling start should populate :instance"
    (with-redefs [d/connect (fn [h p] [h p])]
      (let [server (start (new-server "foobar" 12313123))]
        (is (-> server :instance nil? not))))))

Running the code above with boot watch test throws an error similar to:

Unable to resolve var: d/connect in this context

And then I modify the test code so it requires the db.api

(ns server.core_test
  (:require [server.core :refer :all]
            [clojure.test :refer :all]
            [db.api :as d]))

I ran the tests again, this time d/connect still refers to db.api.

Any advice guys?

Aucun commentaire:

Enregistrer un commentaire