首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Clojure中的乳状基座进行端点单元测试

用Clojure中的乳状基座进行端点单元测试
EN

Stack Overflow用户
提问于 2021-07-29 11:58:15
回答 1查看 106关注 0票数 0

我一直在使用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:

代码语言:javascript
复制
{:queries
 {:hello
  {:type String}}}

core.clj

代码语言:javascript
复制
(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)表示,服务器实例可以通过以下代码片段进行设置和测试:

代码语言:javascript
复制
(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文件)和解析器。

所以我试着调整这个。

代码语言:javascript
复制
(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函数。

EN

回答 1

Stack Overflow用户

发布于 2021-07-30 03:18:52

事实上,我从泪液文献中找到了一个解决方案。https://lacinia.readthedocs.io/en/latest/tutorial/testing-1.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68575488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档