2

我想为 Pedestal Web 服务编写测试。

如果我有 :

(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
4

1 回答 1

2

因此,在询问了我链接的问题后,我ohpaulez回复了:

@nha - 感谢您使用 Pedestal!抱歉,您的问题在 StackOverflow 上没有得到答案 - 我认为没有人会监控 SO 的基座问题。询问这类问题的最佳地点是邮件列表

Pedestal 附带了自己的实用程序,用于直接向 servlet 发出请求(类似于 ring/mock,尽管我自己从未使用过 mock),称为response-for。Pedestal Service 模板会自动为您生成测试。查看其中一个示例作为示例。

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

于 2015-11-24T13:46:58.113 回答