一周前我开始研究clojure,我无法使用默认值为以下代码编写测试用例:
(defn connect-db [{:keys [host port db-name username password]
:or {host "localhost"
port 12345
db-name "my-db"
username "db-user"
password "secret"}
:as cfg}]
(if (= " " host)
"Error: host name not available"
(try
(println "connecting to:" host "port:" port "db-name:" db-name
"username:" username "password:" password)
(catch Exception e(str "caught exception:" (.getMessage e))))))
测试用例应使用deftest编写。
答案 0 :(得分:0)
我不确定测试解构是否有很多好处,因为它是该语言的核心部分,内置于fn
,defn
,let
等等。它工作,只测试你自己的代码。如果你想建立它的预期工作信心,那就在REPL中玩吧。有很多例子here。
我们可以试试这样的默认值:
(let [{:keys [a b]
:or {a "a" b "b"}}
nil]
[a b])
=> ["a" "b"]
答案 1 :(得分:0)
(deftest con-db
(is (= true (map? db)))
(is (= true (function? connectdb)))
(connectdb ["01" "20" "02" "03" "04"] {:host "localhost" :port 8888 :db-name "dbname" :username "name" :password "admin"})
(is(.equals "connecting: remote port: 8888 db-name: dbname username: name password: admin" outputdb))
(is(not(.equals "string" outputdb)))
(is(.equals "01-20" output))
(is(= 400 (- y x)))))