2

I'm writing a Compojure application and am using clj-webdriver to graphically test it. I'm trying to use with-redefs to mock out the function that pulls out data from persistence to just return canned values, but it's ignoring my function overwrite. I know with-redefs works in terms of vars, but it's still not working:

project.clj relevant pieces:

(defproject run-hub "0.1.0-SNAPSHOT"                                                                                                                        
  :main run-hub.handler/start-server)

handler.clj:

(ns run-hub.handler
  (:require [compojure.core :refer :all]
            [compojure.handler :as handler]
            [compojure.route :as route]
            [ring.adapter.jetty :refer :all]
            [run-hub.controllers.log-controller :as log-controller]))

(defroutes app-routes
  (GET "/MikeDrogalis/log" [] (log-controller/mikes-log))
  (route/resources "/")
  (route/not-found "Not Found"))

(def app (handler/site #'app-routes))

log-controller.clj:

(ns run-hub.controllers.log-controller                                                                                                                      
  (:require [run-hub.views.log :as views]
            [run-hub.persistence :as persistence]))

(defn mikes-log []
  (views/mikes-log (persistence/mikes-log)))

persistence.clj

(ns run-hub.persistence                                                                                                                                     
  (require [clj-time.core :as time]
           [run-hub.models.log :as log]))

(defn mikes-log [] [])

And finally, my graphical test - which tries to override mikes-log and fails:

(fact
 "It has the first date of training as August 19, 2012"
 (with-redefs [persistence/mikes-log (fn [] (one-week-snippet))]
   (to (local "/MikeDrogalis/log"))
   (.contains (text "#training-log") "August 19, 2012"))                                                                                                    
 => true)

Where one-week-snippet is a function that returns some sample data. (defn start-server [] (run-jetty (var app) {:port 3000 :join? false}))

Mike
  • 19,267
  • 11
  • 56
  • 72
  • I guess the test call the web server which is a separate process hence your redefs in test (client side) code doesn't effect the var binding on server code – Ankur Sep 05 '12 at 04:24
  • Seems possible, yeah. I'm stumped. – Mike Sep 05 '12 at 14:38
  • The assumption of using clj-webdriver is that you're writing high level integration tests. In my mind these tests should exercise the whole stack so stubbing a function here makes little sense to me. Assuming your individual namespaces have been unit tested, why do you want to stub this function in the integration test? – leonardoborges Mar 12 '13 at 11:54

1 Answers1

0

I am able to use with-redefs in a clj-webdriver test doing the following:

(defn with-server
  [f]
  (let [server (run-jetty #'APP {:port 0 :join? false})
        port (-> server .getConnectors first .getLocalPort)]
    (binding [test-port port]
      (try
        (println "Started jetty on port " test-port)
        (f)
        (finally
          (.stop server))))))

(use-fixtures :once with-server)

Then the whole bunch of tests gets its own jetty and this seems to run in such a manner that with-redefs works.

Kungi
  • 1,477
  • 1
  • 18
  • 34