我正在使用Speclj为Clojure应用程序编写测试。我习惯于在BDD中做这样的事情:
context "some context"
stub my-function :return true
it "has behavior one"
should true my-function
it "has behavior two"
should_not false my-function但在Speclj中,我似乎找不到如何跨特性共享存根的示例,因此,我目前只能编写如下代码:
(describe "this"
(context "that"
(it "accepts nil"
(with-redefs [called-fn (constantly nil)]
(should= nil (my-fn nil)))))
(it "accepts 1"
(with-redefs [called-fn (constantly nil)]
(should= 100 (my-fn 1))))))(我意识到这是一个有些人为的例子,可以说这些断言都有一个特性,但让我们假设现在我有充分的理由编写这样的代码。)
但是,我希望只需要对called-fn进行一次存根,但是将其移出it会引发错误,因为真正的called-fn会被调用,而不是我的参考资料。
有没有一种方法可以重用Speclj中的redefs (或者使用Speclj存根),这样我就不会把它们全部推到特性中去了吗?
发布于 2014-04-17 19:45:15
您可以使用around宏来完成这一任务。
下面是一个示例规范:
(ns sample.core-spec
(:require [speclj.core :refer :all]
[sample.core :refer :all]))
(describe "a test"
(it "returns output from fn-a"
(should= 1 (fn-b)))
(describe "using with-redef"
(around [it] (with-redefs [fn-a (fn [] 2)] (it)))
(it "returns the output from redefined function"
(should= 2 (fn-b)))))来源:
(ns sample.core)
(defn fn-a []
1)
(defn fn-b []
(fn-a))https://stackoverflow.com/questions/23140785
复制相似问题