Overriding a macro operation using Clojure + Midje

Background

I am new to Clojure, so please forgive any blatant mistakes. I am trying to verify a Clojure data access code that uses the redis-clojure library . Although my integration tests, of course, will check the full stack, I do not want my unit tests to depend on the connection to the redis server instance. The offset of the actual Redis commands from Midje seems relatively straightforward, however the join macro is harder to handle.

Required Suggestions

What the documentation cannot seem to find or find through Midje is to mock the redis connection or redefine the macro. The corresponding top-level connection macro from core.clj :

(defmacro with-server
  "Evaluates body in the context of a connection to Redis server
  specified by server-spec.

  server-spec is a map with any of the following keys:
    :host     (\"127.0.0.1\")
    :port     (6379)
    :db       (0)
    :timeout  (5000)
    :password (nil)"
  ([server-spec & body]
     `(with-connection connection# *pool* ~server-spec
        (binding [redis.vars/*channel* (make-direct-channel connection#)]
          ~@body))))

( source code in context here )

I cannot redefine the macro in my test code, and moving the macro to a function does not give me further forwards, since I still need a body that will be executed to get my result. What I ideally would like to do is execute the body passed to the join macro and discard the rest of the macro. Any ideas?

+3
source share
2 answers

Midje ( , , ). , , . ( Freeman Pryce - ( Clojure - , , Midje )), . -, , Redis. , , .

.

, , Midje.

Avdi Grimm Rails - , .

. Redis, Clojure → (with-server). , , , () Ruby ActiveRecord () Sequel (). , , , ( ) . Midje .

+1

. - , . -

(defmacro my-fake-with-server []
   ....)

(defmacro with-fake-with-server
      (binding [redis.core/with-server my-fake-with-server]
               (insert test here)))

(deftest my-test (with-fake-with-server (function-that-uses-with-server)))

.

+2

All Articles