Clojure - 测试 Pedestal 路线

Clojure - test a Pedestal route

我想为 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

所以在询问我链接的问题后 ohpaulez 回复:

@nha - Thanks for using Pedestal! Sorry your question didn't get an answer on Whosebug - I don't think anyone monitors SO for Pedestal questions. The best place to ask those kinds of questions is on the mailing list.

Pedestal ships with its own utility for making requests directly to the servlet (similar to ring/mock, although I've never used mock myself) called response-for. The Pedestal Service template produces a test automatically for you. Check out one of the samples for an example.

另请注意,response-for 尚不支持异步响应(因此我使用异步拦截器和 core.async 的路由失败了 - 我必须让它们同步)。