【问题标题】:Clojure - test a Pedestal routeClojure - 测试基座路线
【发布时间】: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

【问题讨论】:

标签: unit-testing clojure pedestal


【解决方案1】:

所以在询问了这个问题后,我链接了ohpaulez 回复:

@nha - 感谢您使用 Pedestal!抱歉,您的问题没有得到答复 在 StackOverflow 上回答 - 我认为没有人监控 SO 基座问题。问这类问题的最佳地点是 在mailing list

Pedestal 带有自己的实用程序,可以直接向 servlet(类似于 ring/mock,虽然我从未使用过 mock 我自己)叫response-for。 Pedestal Service 模板生成一个 自动为您测试。查看samples 之一以获取 例子。

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

【讨论】:

    猜你喜欢
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多