【发布时间】:2015-11-02 21:49:10
【问题描述】:
我想为 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
【问题讨论】:
-
@ez121sl 我做到了,我已经将它与一个 compojure 应用程序一起使用,让我编辑我的问题。
-
Compojure 的
defroutes创建了一个环处理程序或类似的东西。 Pedestal 的版本显然不同。他们的样本测试没有使用 ring-mock:github.com/pedestal/pedestal/blob/master/samples/hello-world/… -
@ez121sl 感谢您的链接,这看起来像是使用 Pedestal 进行测试的标准方式,但是我很确定 ring.mock (github.com/pedestal/pedestal/issues/319) 也可以,我更喜欢在这一点上保持我的选择开放。
标签: unit-testing clojure pedestal