2015-11-02 102 views
1

我想为Pedestal网络服务编写测试。Clojure - 测试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 
+0

你看过https://github.com/ring-clojure/ring-mock吗? – ez121sl

+0

@ ez121sl我做了,我已经使用它与compojure应用程序,让我编辑我的问题。 – nha

+1

Compojure的'defroutes'创建一个戒指处理器或其他类似的东西。基座的版本显然不同。他们的示例测试不使用ring-mock:https://github.com/pedestal/pedestal/blob/master/samples/hello-world/test/hello_world/service_test.clj – ez121sl

回答

1

所以要求对这个问题后,我联系ohpaulez回答:

@nha - 感谢您使用Pedestal!对不起,你的问题没有得到StackOverflow的 答案 - 我不认为任何人都会监视 基座问题。询问这些问题的最佳地点是 ,mailing list

底座附带了自己的效用直接发出请求 该servlet(类似于环/模拟,虽然我从来没有用过模拟 我)叫response-for。 Pedestal Service模板为您自动生成 测试。查看samples的其中一个 示例。

还要注意的是说response-for还不支持异步响应(所以我的路线是使用异步拦截器与core.async失败 - 我必须让他们同步)。