unit-testing - 如何在clojure中测试用实习生创建的函数?
问题描述
我使用“实习生”函数来生成运行时函数,但我无法测试生成的函数。
在 core.clj 中,我定义了生成这样的运行时函数的函数。
(ns cftf.core)
(defn- generation-function [function-name ratio]
(let [function-symbol (symbol function-name)]
(intern 'cftf.core function-symbol (fn [input] (cftf.core/convert ratio input)))))
...
(defn generate-function-by-units [units]
(->> units
(units->function-name-and-ratio)
(map #(generation-function (first %) (second %)))))
我编写了以下测试代码。
(ns cftf.core-test
(:require [clojure.test :refer :all]
[cftf.core :as c]))
(defn amount-of-alcohol-per-one-bottle [bottle-size abv]
(* bottle-size (/ abv 100)))
(def alcohol-units
{:soju (amount-of-alcohol-per-one-bottle 360 16.5)
:beer (amount-of-alcohol-per-one-bottle 500 5)
:whisky (amount-of-alcohol-per-one-bottle 700 40)
})
(c/generate-function-by-units alcohol-units)
(deftest a-test
(testing "soju->beer test"
(is (= (int (c/soju->beer 1)) 2))))
在repl中调用“generate-function-by-units”后,就可以使用“soju->beer”函数了。
但在测试中我得到错误“没有这样的变量:c/soju->beer”。
我已经尝试使用夹具或在 test.clj 的顶部调用“generate-function-by-units”,但我仍然收到错误。