4

我有许多 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 告诉我,除上述之外的任何内容都不是预期的背景形式。

4

1 回答 1

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))
于 2012-02-11T18:49:15.533 回答