0

以下代码有效(显然):

(use [midje.sweet])

(with-state-changes [(before :contents (println "setup") :after (println "teardown"))]
  (fact "one" 
    (println "doing 1")
    1 => 1)
  (fact "two"
    (println "doing 2")
    (+ 1 1) => 2))

输出是预期的:

setup
doing 1
doing 2
teardown

但我想将我的事实分组在一个单独的函数中,如下所示:

(defn my-facts []
  (fact "one" ...)
  (fact "two" ...)
  #_( ... ))

(with-state-changes [(before :contents (println "setup") :after (println "teardown"))]
  (my-facts))

这次 midje 无法执行代码,我收到以下错误:

Midje could not understand something you wrote: 
        Background prerequisites created by the wrapping version of
        `against-background` only affect nested facts. This one
        wraps no facts.

        Note: if you want to supply a background to all checks in a fact, 
        use the non-wrapping form. That is, instead of this:
            (fact 
              (against-background [(f 1) => 1] 
                (g 3 2 1) => 8 
                (h 1 2) => 7)) 
        ... use this:
            (fact 
              (g 3 2 1) => 8 
              (h 1 2) => 7 
              (against-background (f 1) => 1)) 

有什么方法可以实现我的目标吗?我想使用 midje 的设置和拆卸工具,但仍然能够将我的事实包含在单独的fn.

4

1 回答 1

1

我花了一些时间查看 Midje 源代码,我认为这是不可能的。Midje 在宏扩展期间严重依赖解析和重写代码,并且当它获得您的单独函数调用的结果时,Midje 宏(事实等)已经被扩展和评估。如果你真的下定决心,你可能会做到这一点,但它必须为单独的事实使用宏,而不是函数,以便让 Midje 访问你的原始事实表单。Midje 自始至终都是宏,因此很难与使用其他任何东西进行交互。

于 2015-07-06T13:33:08.977 回答