2016-10-24 35 views
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而另一個接受了另一個呢?

回答

1

您需要具有以下的嵌套,如果你想建立的背景前提條件所有的表格基於事實:

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

如果你想擁有每一個背景先決條件您需要將每個表格盒嵌套如下:

(表格 (反背景 (fa CT ......)) ?...?...)

有剛下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. 

在你的代碼,它看起來像表格數據的位置不正確(括號,括號和大括號不正確,因此很難說究竟是不正確平衡)。

+0

感謝您的回覆。爲什麼是這樣?我應該做些什麼來理解表格和事實之間的區別?我相信嵌套是不是abitrary .... –

+0

我擴大了我的答案,以涵蓋其他可能性。 –