gpt4 book ai didi

testing - 表格测试的测试先决条件;表格如何工作?

转载 作者:行者123 更新时间:2023-11-28 20:28:44 26 4
gpt4 key购买 nike

假设我正在尝试测试一个应该处理某些对象字段是否存在的 api。

假设我有这样的测试:

(def without-foo
{:bar "17"})

(def base-request
{:foo "12"
:bar "17"})

(def without-bar
{:foo "12"})

(def response
{:foo "12"
:bar "17"
:name "Bob"})

(def response-without-bar
{:foo "12"
:bar ""
:name "Bob"})

(def response-without-foo
{:bar "17"
:foo ""
:name "Bob"})

(facts "blah"
(against-background [(external-api-call anything) => {:name => "Bob"})
(fact "base"
(method-under-test base-request) => response)

(fact "without-foo"
(method-under-test without-foo) => response-without-foo)

(fact "without-bar"
(method-under-test without-bar) => response-without-bar))

这正如您所期望的那样工作并且测试通过了。现在我正尝试像这样使用表格重构它:

(def request
{:foo "12"
:bar "17"})

(def response
{:foo "12"
:bar "17"
:name "Bob"})

(tabular
(against-background [(external-api-call anything) => {:name "Bob"})]
(fact
(method-under-test (merge request ?diff) => (merge response ?rdiff))
?diff ?rdiff ?description
{:foo nil} {:foo ""} "without foo"
{} {} "base case"
{:bar nil} {bar ""} "without bar")

结果是:

FAIL at (test.clj:123)
Midje could not understand something you wrote:
It looks like the table has no headings, or perhaps you
tried to use a non-literal string for the doc-string?

最终我得到了:

(tabular
(fact
(method-under-test (merge request ?diff) => (merge response ?rdiff) (provided(external-api-call anything) => {:name "Bob"}))
?diff ?rdiff ?description
{:foo nil} {:foo ""} "without foo"
{} {} "base case"
{:bar nil} {bar ""} "without bar")

哪个通过了。我的问题是。 tabular 函数与 facts 函数有何不同,为什么其中一个接受 against-background 而另一个却爆炸了?

最佳答案

如果您想为所有基于表格的事实建立背景先决条件,则需要进行以下嵌套:

(against-background [...]
(tabular
(fact ...)
?... ?...))

例如:

(require '[midje.repl :refer :all])

(defn fn-a []
(throw (RuntimeException. "Not implemented")))

(defn fn-b [k]
(-> (fn-a) (get k)))

(against-background
[(fn-a) => {:a 1 :b 2 :c 3}]
(tabular
(fact
(fn-b ?k) => ?v)
?k ?v
:a 1
:b 3
:c 3))

(check-facts)
;; => All checks (3) succeeded.

如果您希望每个表格案例都有一个背景先决条件,您需要按如下方式嵌套它:

(表格 (反对背景[...] (事实 ...)) ?...?...)

重要的是让表格处于 tabular 级别,而不是嵌套在 against-backgroundfact 中。

例如:

(require '[midje.repl :refer :all])

(defn fn-a []
(throw (RuntimeException. "Not implemented")))

(defn fn-b [k]
(-> (fn-a) (get k)))

(tabular
(against-background
[(fn-a) => {?k ?v}]
(fact
(fn-b ?k) => ?v))
?k ?v
:a 1
:b 2
:c 3)

(check-facts)
;; => All checks (3) succeeded.

在您的代码中,表格数据的位置似乎不正确(圆括号、方括号和花括号没有正确平衡,因此无法说出到底哪里不正确)。

关于testing - 表格测试的测试先决条件;表格如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40220584/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com