尝试在 clojure 中添加用于方法参数类型检查的单元测试时出现错误

问题描述 投票:0回答:1

我是 clojure 新手,下面是我的代码

(ns my.class.package
    (:require [clojure.tools.logging :as log]
              [schema.core :as s]))

(s/defn my-method :- CandidateInfo [candidates :- [CandidateInfo] goal :- Goal]
  (doseq [candiate candidates]
    (s/validate CandidateInfo candidate))
  (s/validate Goal goal)
  ......)

我进行的单元测试是

(testing "test bad input schema"
           (let [candidates [{:bad-attr1 "123" :bad-attr2 "456"}]
                 goal {:id "123" }]
             (is (thrown? ExceptionInfo
                          (try
                            (my-class/my-method candidates goal)
                            (catch ExceptionInfo e
                              (throw e)))))))

我得到的错误是

ERROR in (test-my-method) (core.clj:155)
Uncaught exception, not in assertion.
expected: nil
  actual: clojure.lang.ExceptionInfo: Value does not match schema: {:id missing-required-key, :name missing-required-key}
at schema.core$validator$fn__14239.invoke (core.clj:155)
    schema.core$validate.invokeStatic (core.clj:164)
    schema.core$validate.invoke (core.clj:159)
    my.class.package.my-class$eval32123$my_method__32128$fn__32129.invoke (my-class.clj:36)
......

我的理解是,我确实期望在单元测试中抛出 ExceptionInfo。然而,错误一直说我期望为零。请帮忙!!

unit-testing clojure
1个回答
0
投票

尝试:

(require '[schema.test :as st])

(st/deftest SchemaTest
    (testing "test bad input schema"
        (let [candidates [{:bad-attr1 "123" :bad-attr2 "456"}]
              goal       {:id "123"}]
            (is (thrown? ExceptionInfo
                    (try
                        (my-class/my-method candidates goal)
                        (catch ExceptionInfo e
                            (throw e))))))))
© www.soinside.com 2019 - 2024. All rights reserved.