模拟多次测试的请求会导致副作用

时间:2018-03-30 21:25:16

标签: clojure leiningen ring

我为我的API创建了2个“功能”测试,当我使用lein test一次运行所有测试时,来自一个测试的请求将在下一个测试用例中传播。 我想知道在运行deftest之后是否有办法解除或撤消请求。

我正在使用ring/ring-mock,使用ring.mock.request/mock模拟请求,我正在使用leingein

运行测试

file1.clj

(deftest some-test
  (testing ""
    (app (-> (mock/request :post "/some-endpoint")
    (mock/content-type "application/json")
    (mock/body (cheshire/generate-string {:some "value"}))))

file2.clj

(deftest some-other-test-file
  (testing ""
    (let [response (app (-> (mock/request :get "/some-endpoint"))]
        ; response has {:some "value"}
)))

1 个答案:

答案 0 :(得分:0)

看起来您想使用Clojure test fixtures。这些允许您使用在测试之前调用的设置函数和在测试之后调用的清理函数来包装测试(单独或作为一组)。

以下是from ClojureDocs.com

的示例
; See https://clojure.github.io/clojure/clojure.test-api.html for details

; my-test-fixture will be passed a fn that will call all your tests 
; (e.g. test-using-db).  Here you perform any required setup 
; (e.g. create-db), then call the passed function f, then perform 
; any required teardown (e.g. destroy-db).
(defn my-test-fixture [f]
        (create-db)
        (f)
        (destroy-db))

; Here we register my-test-fixture to be called once, wrapping ALL tests 
; in the namespace
(use-fixtures :once my-test-fixture)

; This is a regular test function, which is to be wrapped using my-test-fixture
(deftest test-using-db
  (is ... 
))