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中。

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

相关内容

最新更新