在midje重新使用设置和拆除背景

时间:2012-02-11 11:10:56

标签: unit-testing testing clojure midje

我有许多设置/拆卸的中间事实几乎(但不完全)完全相同。

(against-background [(before :contents (setup!)) (before :contents (data)) (before :facts (set-access)) (after :contents (teardown!)]
  (facts "about this thing i am testing "
    ; ...
  ))

(against-background [(before :contents (setup!)) (before :contents (data)) (before :facts (set-other-access)) (after :contents (teardown!)]
  (facts "about this other thing i am testing "
    ; ...
  ))

我想将背景包装成可重复使用的东西,最好是可以重复使用,这样我就可以重复使用它们,但是却无法实现。 Midje告诉我除上述之外的任何事情都不是预期的背景形式。

1 个答案:

答案 0 :(得分:2)

Midje没有能力按照你的要求内置它。如果您愿意,可以考虑将其添加为问题: https://github.com/marick/Midje/issues?sort=updated&direction=desc&state=open&page=1

解决方案是创建自己的宏来执行此操作。 (另)

(defmacro against-my-background [docstring & body]
  `(against-background [(before :contents (setup!)) 
                        (before :contents (data)) 
                        (before :facts (set-access)) 
                        (after :contents (teardown!)]
     (facts ~docstring
       ~@body )))

;; usage
(against-my-background "about this thing i am testing"
  (fact (foo) => :bar)
  (fact (foo) =not=> :baz))