我正在尝试使用 ring-mock 编写 midje 测试来发布到解放者端点。我可以成功地为获取请求编写测试,但我似乎无法将数据传递给帖子,我只能取回格式错误的响应。这是我拥有的代码的核心。
;; ====
; Resource
;; ====
(def users (atom [{:id 1 :name "User One"} {:id 2 :name "User Two"}]))
(defn malformed-users? [context]
(let [params (get-in context [:request :multipart-params])]
(and
(empty? (get params "id"))
(= (get-in context [:request :request-method]) :post))))
(defresource all-users []
:allowed-methods [:get :post]
:available-media-types ["application/json"]
:handle-ok (fn [_] {:users @users})
:malformed? (fn [context] (malformed-users? context))
:handle-malformed "You need to pass both a valid name and id"
:post! (fn [context]
(let [params (get-in context [:request :multipart-params])]
(swap! users conj {:id (bigint (get params "id")) :name (get params "name")})))
:handle-created (fn [_] {:users @users}))
(defroutes user-routes
(ANY "/users" [_] (all-users)))
;; ====
; Test
;; ====
(fact "Get request to users endpoint returns desired content"
(let [response (user-routes (mock/request :post "/users" {:id "3" :name "TestGuy"}))]
(:status response) => 201
(get-in response [:headers "Content-Type"]) => "application/json;charset=UTF-8"))