1

特定のオブジェクト フィールドの有無を処理する 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もう一方が爆発するのはなぜですか?

4

1 に答える 1

2

すべての表ベースのファクトのバックグラウンド前提条件を確立する場合は、次のネストが必要です。

(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.

あなたのコードでは、表形式のデータが正しく配置されていないようです (括弧、括弧、および中括弧のバランスが正しくないため、正確に何が間違っているかはわかりません)。

于 2016-10-24T18:56:37.220 に答える