2013-11-23 25 views
0

在下面的代码中,我想在实现bar函数之前测试foo函数。在Clojure和Midje中,如何编写间接调用的先决条件?

(unfinished bar) 

(def tbl {:ev1 bar}) 
(defn foo [ev] ((tbl ev))) 

(fact "about an indirect call" 
    (foo :ev1) => nil 
    (provided 
    (bar) => nil)) 

但Midje说:

FAIL at (core_test.clj:86) 
These calls were not made the right number of times: 
    (bar) [expected at least once, actually never called] 

FAIL "about an indirect call" at (core_test.clj:84) 
    Expected: nil 
     Actual: java.lang.Error: #'bar has no implementation, 
     but it was called like this: 
(bar) 

我认为“提供”不能勾栏功能,因为富并没有直接调用了吧。但我也发现,如果我改变了这样的第二行:

(def tbl {:ev1 #(bar)}) 

然后测试成功。

有什么办法可以成功的第一个版本?

谢谢。 PS:我使用的是Clojure 1.5.1和Midje 1.5.1。

回答

0

provided使用with-redefs的风味来改变var的根。定义tbl时,您已经取消引用了变量#'bar,提示tbl包含未绑定的值,而不是对要执行的逻辑的引用。你不能提供已经评估过的值的替代品就是我想说的。

同理:

(defn x [] 0) 
(def tbl {:x x}) 
(defn x [] 1) 
((:x tbl)) ;; => 0 

你需要的是对VAR本身存储在tbl

(defn x [] 0) 
(def tbl {:x #'x}) 
(defn x [] 1) 
((:x tbl)) ;; => 1 

同样为你的测试。一旦你使用(def tbl {:ev1 #'bar}),他们通过。