单元测试 – 在midje中重复使用设置和拆除背景

我有一些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告诉我除上述之外的任何事情都不是预期的背景形式.

最佳答案 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))
点赞