Testing moustache routes

19 views
Skip to first unread message

Giorgio Valoti

unread,
Feb 25, 2011, 4:36:44 AM2/25/11
to clojure...@googlegroups.com
Hi,
I’m trying to find the best way to test route handling with moustache <https://github.com/cgrand/moustache>, without generating the whole response.

Instead of:
(app
[""] (fn [req] {:status 200 :header {...} :body}))

Ideally, I’d like to write something like:
(app
[""] {:page-name "index"})

and then use the :page-name to build my template. At the same time I can just assert that given a URL like "/" the expected page is called "index".

Is there a way to do this? Am I completely off track?

Thank you in advance
--
Giorgio Valoti

Saul Hazledine

unread,
Feb 25, 2011, 5:45:48 AM2/25/11
to Clojure Web Development
On Feb 25, 10:36 am, Giorgio Valoti <giorgi...@me.com> wrote:
> Hi,
> I’m trying to find the best way to test route handling with moustache <https://github.com/cgrand/moustache>, without generating the whole response.
>

For actual unit testing its worth looking at ring-mock:
https://github.com/weavejester/ring-mock

> Instead of:
> (app
>     [""] (fn [req] {:status 200 :header {...} :body}))
>
> Ideally, I’d like to write something like:
> (app
>     [""] {:page-name "index"})
>
> and then use the :page-name to build my template. At the same time I can just assert that given a URL like "/" the expected page is called "index".
>

I don't fully understand what you intend to do but I believe that you
could write some ring middleware that takes a request, the response
from moustache and combines them in some way (perhaps by filling in an
Enlive template?).

(defn build-template [app]
(fn [req]
(if-let [{:keys [page-name]} (app req)]
(do-something-clever page-name req)
{:status 404 :body "Not found"})))


Saul

Giorgio Valoti

unread,
Feb 25, 2011, 6:08:23 AM2/25/11
to clojure...@googlegroups.com

My intent is testing moustache routes in isolation from page generation. I thought about using a middleware solution but I was unsure about it: would it be a good / idiomatic use of middleware?


--
Giorgio Valoti

James Reeves

unread,
Feb 25, 2011, 6:28:24 AM2/25/11
to clojure...@googlegroups.com
On 25 February 2011 11:08, Giorgio Valoti <gior...@me.com> wrote:
> My intent is testing moustache routes in isolation from page generation. I thought about using a middleware solution but I was unsure about it: would it be a good / idiomatic use of middleware?

I think what you want here is to use a combination of ring-mock to
mock your requests, and something like clojure.contrib.mock or midje
to mock the functions your routes call.

For example:

(def handler
(app [""] index-handler))

(deftest test-routes
(expect [index-handler (times 1)]
(handler (request :get "/")))))

- James

Giorgio Valoti

unread,
Feb 25, 2011, 7:46:02 AM2/25/11
to clojure...@googlegroups.com


Sounds like the perfect solution. Thank you, James!

Ciao
--
Giorgio Valoti

Reply all
Reply to author
Forward
0 new messages