简体   繁体   中英

testing clojure luminus application with midje

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM