我一直在使用Pedestal进行RESTful API服务器及其端点单元测试。这种方法是设置服务器并在端点级别上测试它。所谓的“端点单元测试”在下面的页面中有很好的说明。
http://pedestal.io/reference/unit-testing#_testing_your_service_with_response_for
然而,这一次,我将Lacinia-Pedestal用于GraphQL (换句话说,不是RESTful ),我想知道是否可以应用相同的端点测试逻辑。在乳座知识库(https://github.com/walmartlabs/lacinia-pedestal)中,我找不到相关的指令.事实上,它根本没有提到单元测试。
如果有人有这种方法的经验,你能分享吗?谢谢!
-编辑-我在这里添加我的测试代码
resources/main-schema.edn:
{:queries
{:hello
{:type String}}}core.clj
(ns lacinia-pedestal-lein-clj.core
(:require [clojure.edn :as edn]
[clojure.java.io :as io]
[com.walmartlabs.lacinia.pedestal2 :as p2]
[com.walmartlabs.lacinia.schema :as schema]
[com.walmartlabs.lacinia.util :as util]
[io.pedestal.http :as http]))
(defn resolve-hello
[_ _ _]
"hello, darren")
(defn hello-schema
[]
(-> (io/resource "main-schema.edn")
slurp
edn/read-string
(util/inject-resolvers {:queries/hello resolve-hello})
schema/compile))
(def service
(-> (hello-schema)
(p2/default-service nil)
http/create-server
http/start))Pedestal (而不是Lacinia-Pedestal)表示,服务器实例可以通过以下代码片段进行设置和测试:
(ns lacinia-pedestal-lein-clj.core-test
(:require [lacinia-pedestal-lein-clj.core :as core]
[clojure.test :as t]
[io.pedestal.http :as http]
[io.pedestal.test :as ptest]
[com.walmartlabs.lacinia.pedestal2 :as p2]
[com.walmartlabs.lacinia.util :as util]))
(def service
(:io.pedestal.http/service-fn
(io.pedestal.http/create-servlet service-map)))
(is (= "Hello!" (:body (response-for service :get "/hello"))))但是,我认为这种方法适用于RESTful,但不适用于GraphQL,因为GraphQL需要为服务器设置模式(.edn文件)和解析器。
所以我试着调整这个。
(ns lacinia-pedestal-lein-clj.core-test
(:require [lacinia-pedestal-lein-clj.core :as core]
[clojure.test :as t]
[io.pedestal.http :as http]
[io.pedestal.test :as ptest]
[com.walmartlabs.lacinia.pedestal2 :as p2]
[com.walmartlabs.lacinia.util :as util]))
(defonce service
(-> (core/hello-schema)
(p2/default-service nil)
http/create-server
http/start))
(t/is (= "Hello!"
(:body
(util/response-for service
:get "/hello")))) ;; NOT WORKING但是它不能这样工作,因为response-for需要interceptor-service-fn类型。因此,据我所知,真正的问题是如何在response-for服务器实例中使用GraphQL函数。
发布于 2021-07-30 03:18:52
事实上,我从泪液文献中找到了一个解决方案。https://lacinia.readthedocs.io/en/latest/tutorial/testing-1.html
https://stackoverflow.com/questions/68575488
复制相似问题