Midje,如果没有按我的预期工作

发布于 2024-12-15 07:53:28 字数 870 浏览 5 评论 0原文

我编写了以下 Midje 测试:

(fact (followers-minus-friends ...name...) => ["Dude"]
      (provided (idset show-followers ...name...) => #{1 2 3}
                (idset show-friends ...name...) => #{1 2}
                (userinfos #{3}) => [{:screen_name "Dude"}]))

测试以下函数(在不同的命名空间中):

(defn followers-minus-friends [screenname]
  (let [difference-ids (difference (idset show-followers screenname)
                                   (idset show-friends screenname))
        userinfos (userinfos difference-ids)]
    (map :screen_name userinfos)))

该测试可能看起来毫无用处,但我只是想习惯 Midje。不知何故,函数 idset 就被执行了,我想通过在提供的子句中提供返回值来阻止这种情况。对此有何解释?

编辑:我已将项目上传到 Github,以防您想尝试重现上述情况:https://github.com/Borkdude/twitter-utils

I have written the following Midje test:

(fact (followers-minus-friends ...name...) => ["Dude"]
      (provided (idset show-followers ...name...) => #{1 2 3}
                (idset show-friends ...name...) => #{1 2}
                (userinfos #{3}) => [{:screen_name "Dude"}]))

to test the following function (in a different namespace):

(defn followers-minus-friends [screenname]
  (let [difference-ids (difference (idset show-followers screenname)
                                   (idset show-friends screenname))
        userinfos (userinfos difference-ids)]
    (map :screen_name userinfos)))

The test may seem pretty useless, but I'm just trying to get accustomed to Midje. Somehow, the function idset just gets executed, which I wanted to prevent by providing a return value in the provided-clause. What could be an explanation for this?

EDIT: I have uploaded the project to Github here, in case you want to try to reproduce the above situation: https://github.com/Borkdude/twitter-utils

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌无敌 2024-12-22 07:53:28

问题在于函数 idset 在源命名空间中是私有的。在测试文件中,您将其保留在测试命名空间中。现在,您有两个指向同一个函数的不同变量:

 #'twitter-utils.core/idset
 #'twitter-utils.test.core/idset

provided中,您将覆盖第二个变量以指向先决条件函数。然而,函数followers-minus-friends仍然引用第一个变量。这没有被覆盖,所以你得到了原始的函数。

围绕 private 声明进行路由的常见习惯用法是使用完整的 var 名称,如上所述。所以你会期望这能起作用:

(fact (followers-minus-friends ...name...) => ["Dude"]
      (provided (#'twitter-utils.core/idset show-followers ...name...) => #{1 2 3}
                (#'twitter-utils.core/idset show-friends ...name...) => #{1 2}
                (userinfos #{3}) => [{:screen_name "Dude"}]))

但事实并非如此。我认为这是 Midje 的一个错误。至少,我暂时看不出支持这样做会带来什么危害。我会开一张票。

按照目前的情况,您需要将 idset 公开。

The problem is that the function idset is private in your source namespace. In the test file, you intern it in the test namespace. You now have two different vars that point to the same function:

 #'twitter-utils.core/idset
 #'twitter-utils.test.core/idset

In the provided, you are overriding the second var to point to a prerequisite function. However, the function followers-minus-friends still refers to the first var. That is not overridden, so you get the original function.

A common idiom for routing around private declarations is to use the full var name, as above. So you'd expect this to work:

(fact (followers-minus-friends ...name...) => ["Dude"]
      (provided (#'twitter-utils.core/idset show-followers ...name...) => #{1 2 3}
                (#'twitter-utils.core/idset show-friends ...name...) => #{1 2}
                (userinfos #{3}) => [{:screen_name "Dude"}]))

It doesn't, though. I consider that a bug in Midje. At least, I can't offhand see what harm supporting that would do. I'll file a ticket.

As it stands, you need to make idset public.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文