5

我正在使用 Stuart Sierra 组件实现一个应用程序。正如他在自述文件中所说:

有一种连贯的方式来设置和拆除与应用程序相关的所有状态,可以在不重新启动 JVM 的情况下实现快速的开发周期。它还可以使单元测试更快、更独立,因为创建和启动系统的成本足够低,以至于每个测试都可以创建系统的新实例。

这里的首选策略是什么?类似于 JUnit oneTimeSetUp / oneTimeTearDown 的东西,或者真的在每个测试之间(类似于 setUp / tearDown)?

如果在每次测试之间,是否有一种简单的方法可以为所有测试(之前和之后)启动/停止系统,而无需每次都重复代码?

编辑:示例代码显示我的意思

(defn test-component-lifecycle [f]
  (println "Setting up test-system")
  (let [s (system/new-test-system)]
    (f s) ;; I cannot pass an argument here ( https://github.com/clojure/clojure/blob/master/src/clj/clojure/test.clj#L718 ), so how can I pass a system in parameters of a test ?
    (println "Stopping test-system")
    (component/stop s)))

(use-fixtures :once test-component-lifecycle)

注意:我在这里谈论单元测试。

4

1 回答 1

0

我会编写一个宏,它采用系统映射并在运行测试之前启动所有组件并在测试后停止所有组件。

例如:

(ns de.hh.new-test
 (:require [clojure.test :refer :all]
           [com.stuartsierra.component :as component]))


;;; Macro to start and stop component
(defmacro with-started-components [bindings & body]
    `(let [~(bindings 0) (component/start ~(bindings 1))]
       (try
          (let* ~(destructure (vec (drop 2 bindings)))
            ~@body)
       (catch Exception e1#)
       (finally
         (component/stop ~(bindings 0))))))

;; Test Component
(defprotocol Action
  (do-it [self]))

(defrecord TestComponent [state]
   component/Lifecycle
   (start [self]
      (println "====> start")
      (assoc self :state (atom state)))
   (stop [self]
      (println "====> stop"))

   Action
    (do-it [self]
       (println "====> do action")
       @(:state self)))

;: TEST
(deftest ^:focused component-test
   (with-started-components
      [system (component/system-map :test-component (->TestComponent"startup-state"))
       test-component (:test-component system)]

    (is (= "startup-state" (do-it test-component)))))

运行测试你应该看到这样的输出

====> start
====> do action
====> stop

Ran 1 tests containing 1 assertions.
0 failures, 0 errors.
于 2017-08-11T13:15:51.620 回答