Midje的`with-state-changes`和函数调用

时间:2015-07-06 10:38:10

标签: clojure midje

以下代码(显然)有效:

(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内。

1 个答案:

答案 0 :(得分:1)

我花了一些时间查看Midje源代码,我不认为这是可能的。 Midje在宏扩展期间严重依赖于解析和重写代码,并且当它获得单独函数调用的结果时,Midje宏(事实等)将已经被扩展和评估。如果你真的有决心,你可能会发生这种情况,但它必须使用一个宏来处理单独的事实,而不是函数,以便让Midje访问你的原始事实表格。 Midje几乎都是宏,因此很难与其他任何东西进行交互。