简体   繁体   English

Clojure - 测试一个 Pedestal 路由

[英]Clojure - test a Pedestal route

I would like to write tests for a Pedestal web-service.我想为 Pedestal 网络服务编写测试。

If I have :如果我有 :

(defn pong
  [request]
  (ring-resp/response "pong"))

(defroutes routes[[["/" {:get pong}]]])

How would I write a test for that ?我将如何为此编写测试?

(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.
    ...)

Edit : Here is what I tried :编辑:这是我尝试过的:

(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")))))

But I get an exception :但我得到一个例外:

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

So after asking on the issue I linked ohpaulez replied :所以在询问这个问题后,我联系了ohpaulez回答:

@nha - Thanks for using Pedestal! @nha - 感谢您使用 Pedestal! Sorry your question didn't get an answer on StackOverflow - I don't think anyone monitors SO for Pedestal questions.抱歉,您的问题没有在 StackOverflow 上得到答案 - 我认为没有人会针对 Pedestal 问题监控 SO。 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 . Pedestal 附带了它自己的实用程序,用于直接向 servlet 发出请求(类似于 ring/mock,虽然我自己从未使用过模拟),称为response-for The Pedestal Service template produces a test automatically for you.基座服务模板会自动为您生成测试。 Check out one of the samples for an example.查看其中一个示例以获取示例。

Also note that said response-for doesn't yet support asynchronous responses (so my routes that uses asynchronous interceptors with core.async failed - I had to make them synchronous).另请注意,所述response-for尚不支持异步响应(因此,我使用带有core.async异步拦截器的core.async失败了 - 我必须使它们同步)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM