gpt4 book ai didi

unit-testing - clojure 单元测试与重定义

转载 作者:行者123 更新时间:2023-12-02 03:24:29 25 4
gpt4 key购买 nike

我有这样的事情:

(ns server.core
(:require [db.api :as d]))

(defrecord Server [host port instance]
(start [c]
(let [connection (d/connect (:host c) (:port c))]
(assoc c :instance connection)))
(stop [c]
;; close the connection
))

(defn new-server
[host port]
(map->Server {:host host
:port port}))

和单元测试代码
(ns server.core_test
(:require [server.core :refer :all]
[clojure.test :refer :all]))

(deftest server-test
(testing "Calling start should populate :instance"
(with-redefs [d/connect (fn [h p] [h p])]
(let [server (start (new-server "foobar" 12313123))]
(is (-> server :instance nil? not))))))

使用 boot watch test 运行上面的代码引发类似于以下内容的错误:
Unable to resolve var: d/connect in this context
然后我修改了测试代码,所以它需要 db.api
(ns server.core_test
(:require [server.core :refer :all]
[clojure.test :refer :all]
[db.api :as d]))

我再次进行了测试,这次是 d/connect仍指 db.api .

有什么建议吗?

最佳答案

您需要要求 db.api在您的测试代码中,var d/connect可访问:

(ns server.core_test
(:require [server.core :refer :all]
[db.api :as d]
[clojure.test :refer :all]))

(deftest server-test
(testing "Calling start should populate :instance"
(with-redefs [d/connect (fn [h p] [h p])]
(let [server (start (new-server "foobar" 12313123))]
(is (-> server :instance nil? not))))))

关于unit-testing - clojure 单元测试与重定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31157889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com