将值传递给每个speclj规范?

时间:2016-03-06 14:29:51

标签: clojure speclj

我希望在每个规范之前启动服务,并在每个规范之后将其关闭。同时我希望每个规范都能够使用规范中的service。例如(它不起作用):

(describe
  "Something"

  (around [it]
          (let [service (start!)]
            (try
              (it)
              (finally
                (shutdown! service)))))

  (it "is true"
      ; Here I'd like to use the "service" that was started in the around tag
      (println service) 
      (should true))

  (it "is not false"
      (should-not false)))

我该怎么做?

1 个答案:

答案 0 :(得分:1)

我无法在speclj中看到它的直接支持,并且其内部设计不允许使用此类功能扩展它。但是,您可以使用动态范围来实现它:

(declare ^:dynamic *service*)

(describe
  "Something"

  (around [it]
    (binding [*service* (start!)]
      (try
        (it)
        (finally
          (shutdown! *service*)))))

  (it "is true"
    (println *service*) 
    (should true))

  (it "is not false"
    (should-not false)))

*service* var将绑定到(start!)范围内binding的结果。