Question

Background

I'm new to Clojure so please forgive any glaring errors. I am trying to test some Clojure data access code that uses the redis-clojure library. Whilst my integration tests will, of course, test the full stack I do not want my unit tests to be reliant upon connecting to a redis server instance. Mocking the actual Redis commands with Midje appears to be relatively straight-forward however, the connection macro is more difficult to deal with.

Suggestions needed

What can't seem to do or find via the Midje documentation is way of mocking the redis connection or redefining the macro. The relevant top level connection macro from core.clj is:

(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))))

(original code in context here)

I don't appear to be able to redefine the macro in my test code and wrapping the macro in a function doesn't get me any further forwards as I still need the body to be executed to produce my result. What I'd ideally like to do is execute the body passed into the connection macro and discard the rest of the macro. Any ideas?

Was it helpful?

Solution

I suggest asking your question on the Midje mailing list (and then, of course, posting the final answer here). Without knowing what you're trying to test, it's hard to give advice. Perhaps (following Freeman and Pryce's Growing Object-Oriented Software (which does apply to Clojure - indeed, supporting that style was the reason I wrote Midje)), you want to separate your problem into two parts. First, you want to build a set of functions that isolate Redis. You test that those functions work correctly with slow and cumbersome direct tests that work with the real database.

Those functions would hide the connection and the need for a wrapping macro.

Then you can test that your code calls the isolation functions correctly, using Midje prerequisites.

Avdi Grimm's Objects on Rails is another book that does a decent job of talking about isolating the database.

Note: I don't know anything about Redis, but several other Clojure->database libraries dispense with the intellectually appealing wrapping-macros (with-server). Instead, they go the stateful route and have a global variable that holds the connection, much like (say) Ruby database libraries ActiveRecord (implicitly) and Sequel (explicitly) do. Despite being less pure, it seems to make life (including testing) simpler. In Midje, that connection could be represented by a metaconstant.

OTHER TIPS

I often write binding wrapper macros for testing. These macros just bind something else to the function or macro that can't be tested. In this case I would write something along these lines

(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)))

This keeps all the mocking in your test code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top