我有许多 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))
关于unit-testing - 在 midje 中使用背景重用设置和拆卸,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9239728/