0

I want to test my Compojure endpoints and see that the required method is called. However, with-redefs-fn is not replacing the method.

Test method-

(ns project-name.handler-test
  (:require [clojure.test :refer :all]
        [project-name.handler :as handler :refer [retrieve-records test-handler]]
        [ring.mock.request :as mock]))

(deftest fn-handler-routes
  (with-redefs-fn {#'handler/retrieve-records (fn [arg] :test)} 
    (let [response {:status 200 :body :test}] 
    (let [mock-handler (test-handler)]
    (is (= response (mock-handler (mock/request :get "/records/name"))))))))

I am guessing routes are static, and thus the method fires and returns actual data instead of calling the redefined function. test-handler is a failing attempt to try to circumvent -- it fires okay, but does not use with-redefs and returns repo data.

Pertinent Source Code-

(defn retrieve-records [arg] (repo/get-records arg))

(defroutes routes
  (GET "/" [] (resource-response "index.html" {:root "public"}))
  (context "/records" []
    (GET "/name" [] (retrieve-records :lastname)))
  (resources "/"))

(defn test-handler []
  (-> #'routes wrap-reload))

Related Issues

I suspect a similar underlying issue as this SO question about Midje but I am using clojure.test and not midje.

This question is different from a very similar SO question in that the Compojure route is legitimate and I want to return something in the :body, not just test the status (already tried that answer).

This question is different from an integration test question in that it is a handler unit test, though probably the same underlying issue.

Any help would be greatly appreciated.

Community
  • 1
  • 1
nrako
  • 2,952
  • 17
  • 30
  • Why won't you use `with-redefs` instead of `with-redefs-fn`? – OlegTheCat Aug 08 '16 at 19:46
  • 1
    Second argument to `with-redefs-fn` should be a function, not actual code. See example in the doc: https://clojuredocs.org/clojure.core/with-redefs-fn – OlegTheCat Aug 08 '16 at 19:47
  • So I am not 100% sure of what you are suggesting. If you could drop it in an answer, I will give it a try and can accept as answered. – nrako Aug 08 '16 at 20:49
  • I mean that `with-redefs-fn` should be used as follows: `(with-redefs-fn {} (fn [] )`. Now you are using it like `(with-redefs-fn {} )` (i.e. wrapping body with `fn` is missing). Doesn't this cause the problem? – OlegTheCat Aug 10 '16 at 08:26

0 Answers0