testing clojure luminus application with midje

146 views Asked by At

I'm doing tests on my luminus application and I want to test my post fuction as below. However, the data is posted on the body of the request object as a byte input stream. How do i make the data to be posted on the params key of the request object? I got this example from this link http://www.jarrodctaylor.com/posts/Compojure-Address-Book-Part-1/

(defn example-post [request]
  (let [post-value (get-in request [:params :example-post])]
    (str "You posted: " post-value)))

  (fact "Test POST"
    (let [response (app (mock/request :post "/post" {:example-post "Some data"}))]
      (:status response) => 200
      (:body response) => "You posted: Some data")))
1

There are 1 answers

0
joeabala On

Got the answer, I was binding the mock/request in the ring handler function defroutes app-routes as opposed to the app var:

(defroutes app-routes
           (GET "/" [] tests)
           (POST "/post" [] example-post)
           (not-found "invalid request"))

(def app
  (wrap-defaults app-routes (assoc-in site-defaults [:security :anti-forgery] false)))

The correct way:

 (fact "Test POST"
    (let [response (app (mock/request :post "/post" {:example-post "Some data"}))]
      (:status response) => 200
      (:body response) => "You posted: Some data")))

Incorrect way

 (fact "Test POST"
    (let [response (app-routes (mock/request :post "/post" {:example-post "Some data"}))]
      (:status response) => 200
      (:body response) => "You posted: Some data")))