将值传递给每个 speclj 规范?

Pass value to each speclj spec?

我想在每个规范之前启动服务并在每个规范之后关闭它。同时,我希望每个规范都能够使用规范中的 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)))

我该怎么做?

我在 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* 变量将在 binding 范围内绑定到 (start!) 的结果。