0

我正在测试一个使用其他命名空间中的其他两个函数的函数。

(fact "a test"
      (let [result (function-that-uses-functions-from-other-namespace)]
      result => truthy))

我想从其他命名空间中提取函数,并且在编写测试时遇到问题。

(fact "a test"
      (let [result (function-that-uses-functions-from-other-namespace)]
          (function-that-uses-functions-from-other-namespace)
          (provided (other-namespace/function1 "parameter") => "stub_response"))

但这种方法似乎不起作用。有什么提示吗?是否只是在使用在 let 子句中提供的 midje 中建议的可检查之前评估let 的情况不存根方法

4

3 回答 3

4

您可以使用against-background它来消除函数调用,不仅针对单个事实,而且针对整个fact表单:

(fact "a test"
      ...
      (against-background
        (other-namespace/function1 "parameter") => "stub_response"))

实际上,这可能也适用于您链接的问题。

于 2014-04-09T15:58:54.293 回答
0

在设置先决条件之前尝试不使用它,let因为它会立即执行该功能:

(fact "a test"
  (function-that-uses-functions-from-other-namespace) => truthy
  (provided (some-internal-func arg1 arg2) => some-result
            (other-internal-func) => other-result))

你只需要有被存根的函数required. 它们可以来自另一个命名空间。

请注意,协议方法不能以这种方式存根。

于 2014-04-10T09:15:26.603 回答
0

它可以使用(prerequisite)吗?像这样的东西:

(fact "a test"
  (prerequisite (other-namespace-function1 "parameter) => "stub_response")
  (function-that-uses-functions-from-other-namespace) => truthy))

这应该让您可以为您喜欢的任何旧函数指定返回值,当您运行实际事实时,这些函数将被重放。

看看fact-wide-prerequisites。我更喜欢这个provided,因为它让我坚持一种既定的测试风格,仍然使用与实际检查器相同的语法。

于 2014-04-11T16:52:48.637 回答