2016-02-18 31 views
0

我正在做我的luminus應用測試,我想測試我的後功能如下。但是,數據作爲字節輸入流發佈在請求對象的主體上。我如何使數據發佈在請求對象的params鍵上?我從這個鏈接http://www.jarrodctaylor.com/posts/Compojure-Address-Book-Part-1/測試與midje clojure luminus應用

(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"))) 
+0

你的問題與Midje毫無關係。如果'app'中沒有'params'中間件,':params'將不會被填充。 – muhuk

+0

當我執行我的正常請求時,數據存儲在請求對象的params鍵中,所以我只是使用( - > req:params:data)解析它,所以我不認爲這可能是問題。 – joeabala

+0

你說得對@muhuk,非常感謝 – joeabala

回答

0

得到的回答這個例子中,在環處理函數,我結合模擬/請求defroutes應用的路由,而不是應用程序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))) 

的正確的方法:

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

不正確的方式

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