1

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

我怎样才能做到这一点?

4

1 回答 1

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)))

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

于 2016-03-06T15:16:21.377 回答