1
我想爲Pedestal網絡服務編寫測試。Clojure - 測試Pedestal路線
如果我有:
(defn pong
[request]
(ring-resp/response "pong"))
(defroutes routes[[["/" {:get pong}]]])
我怎麼會寫一個測試?
(deftest alive-system
(testing "ping-pong route"
;; How do I test my route ?
;; If possible :
;; - I would like to have direct access to it
;; ie. no need to bind pedestal to a port would be nice
;; - The ability to omit some interceptors would be nice also,
;; as it would allow me to receive plain Clojure data structures
;; instead of, for instance, JSON which I would have to parse.
...)
編輯: 這裏是我的嘗試:
(deftest alive-system
(testing "ping-pong route"
(let [response (my-other.ns/routes (mock/request :get "/ping"))]
(is (= (:status response) 200))
(is (= (:body response) "pong")))))
但我得到一個異常:
ERROR in (alive-system) (service_test.clj:13)
Uncaught exception, not in assertion.
expected: nil
actual: java.lang.ClassCastException: clojure.lang.LazySeq cannot be cast to clojure.lang.IFn
你看過https://github.com/ring-clojure/ring-mock嗎? – ez121sl
@ ez121sl我做了,我已經使用它與compojure應用程序,讓我編輯我的問題。 – nha
Compojure的'defroutes'創建一個戒指處理器或其他類似的東西。基座的版本顯然不同。他們的示例測試不使用ring-mock:https://github.com/pedestal/pedestal/blob/master/samples/hello-world/test/hello_world/service_test.clj – ez121sl