0

我正在对我的 luminus 应用程序进行测试,我想测试我的后期功能,如下所示。但是,数据作为字节输入流发布在请求对象的主体上。如何使数据发布在请求对象的 params 键上?我从这个链接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")))
4

1 回答 1

0

得到了答案,我在环处理函数中绑定了模拟/请求,defroutes app-routes 而不是 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)))

正确的方法:

 (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")))
于 2016-02-18T13:34:54.653 回答