Skip to content

How To Get Midje Working With appengine-magic

July 4, 2012

Recently I’ve begun using a testing framework in Clojure called Midje. There are several other Clojure testing libraries including one that comes with Clojure. Midje does have an interesting syntax however and good support for mocking – although it doesn’t use that term. One particularly nice thing is it’s integration with Emacs. With quick keyboard shortcut I can easily run the current Midje test and see the results come up as comments right next to the test. Those comments can then be easily cleared once I’m done. This is all covered in a good amount of detail on the website. The video in particular is particularly good at showing the sorts of things that Midje is good at.

One problem I ran into immediately is how to integrate it with appengine-magic. The appengine-magic library comes with testing infrastucture to allow you to test your App Engine projects outside a container. By default AEM only includes support for clojure test however with a bit of work it can be made to work with Midje.

I did manage to find a reference to a code snippet on the Midje mailing list but couldn’t get it to work as written. With some small modifications however I managed to get it working.

Firstly we need to define a macro that wraps the setup and teardown work required to get Midje to play nicely with the App Engine testing infrastructure.

(defmacro midje-magic [?form & helpers]
  (let [helper-sym (gensym "helper")]
    `(let [environment# (ApiProxy/getCurrentEnvironment)
           delegate# (ApiProxy/getDelegate)
           ~helper-sym (LocalServiceTestHelper. (into-array LocalServiceTestConfig (local-services-helper)))
           ~@(apply concat
                    (for [helper helpers]
                      (list helper-sym (list helper helper-sym))))]
       (.setUp ~helper-sym)
       ~?form
       (.tearDown ~helper-sym)
       (ApiProxy/setEnvironmentForCurrentThread environment#)
       (ApiProxy/setDelegate delegate#))))

The ?form reference is used to by the background references to insert the test code as it executes. The helpers are optional and allow you to specify appengine-magic test helpers that you wish to be active. With this macro defined we can simply use Midjes normal background declarations to initialize the appengine-magic testing infrastructure. For example:

(background
  (around :facts
          (midje-magic ?form)))

If you wanted to use some helpers you could do something like this:

(background
  (around :facts
          (midje-magic ?form
                       (ae-testing/login "someone@mailhost.com"))))

If you want to get a flavour of what Midje looks like Brian has put together some excellent videos. Links can be found on the Midje wiki:

https://github.com/marick/Midje/wiki

If Midje isn’t your thing there are plenty of altnerative testing frameworks to use. You can find a reasonably up-to-date list at the Clojure Libaries website under the testing category 1.

Advertisements

From → Clojure, Technical

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: