【问题标题】:Dynamically calculated description of a midje factmidje 事实的动态计算描述
【发布时间】:2013-12-29 17:24:05
【问题描述】:
我想写一个函数来分解一些常见的事实,像这样
(defn check-odd-and-positive
[n]
(fact (str n " not odd") n => odd?)
(fact (str n " not positive") n => positive?))
(facts "about the answer"
(check-odd-and-positive 42))
但它不会导致“42 不奇数”作为事实的描述。我知道使用表格事实可以达到类似的效果,但我希望能够在事实组之间分享这样的事实。
【问题讨论】:
标签:
testing
clojure
midje
【解决方案1】:
我发现,从 midje 1.6 开始,metadata 非常简单
(fact {:midje/description (str n "not odd")} n => odd?)
【解决方案2】:
你可以在这里使用宏
(defmacro check-odd-and-positive [n]
`(fact ~(str n " not odd") n => odd?)
`(fact ~(str n " not positive" n => positive?))
但是,midje 在报告中包含了测试值,所以我根本无法清楚地看到为什么这是必要的。