在 midje 中通过背景重用设置和拆卸

发布于 2025-01-05 03:15:50 字数 545 浏览 4 评论 0原文

我有许多 midje 事实,它们的设置/拆卸几乎完全相同,但不完全相同。

(against-background [(before :contents (setup!)) (before :contents (data)) (before :facts (set-access)) (after :contents (teardown!)]
  (facts "about this thing i am testing "
    ; ...
  ))

(against-background [(before :contents (setup!)) (before :contents (data)) (before :facts (set-other-access)) (after :contents (teardown!)]
  (facts "about this other thing i am testing "
    ; ...
  ))

我想将背景包装成可重用且最好可参数化的东西,以便我可以重用它们,但这样做遇到困难。 Midje 告诉我除上述以外的任何内容都不是预期的背景形式。

I have a number of midje facts that have setup/teardowns that are almost, but not quite, entirely the same.

(against-background [(before :contents (setup!)) (before :contents (data)) (before :facts (set-access)) (after :contents (teardown!)]
  (facts "about this thing i am testing "
    ; ...
  ))

(against-background [(before :contents (setup!)) (before :contents (data)) (before :facts (set-other-access)) (after :contents (teardown!)]
  (facts "about this other thing i am testing "
    ; ...
  ))

I would like to wrap the backgrounds into something reusable and preferably paramterizable so I can reuse them, but having trouble doing so. Midje tells me anything other than the above is not the expected background form.

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

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

发布评论

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

评论(1

╰つ倒转 2025-01-12 03:15:50

Midje 没有能力执行您内置的要求。如果您愿意,请考虑将其添加为此处的问题:
https://github.com/marick /Midje/issues?sort=updated&direction=desc&state=open&page=1

解决方案是创建您自己的宏来执行此操作。 (未经测试)

(defmacro against-my-background [docstring & body]
  `(against-background [(before :contents (setup!)) 
                        (before :contents (data)) 
                        (before :facts (set-access)) 
                        (after :contents (teardown!)]
     (facts ~docstring
       ~@body )))

;; usage
(against-my-background "about this thing i am testing"
  (fact (foo) => :bar)
  (fact (foo) =not=> :baz))

Midje does not have the ability to do what you ask built into it. If you would like this, consider adding it as an issue here:
https://github.com/marick/Midje/issues?sort=updated&direction=desc&state=open&page=1

A solution is to create your own macro to do this. (untested)

(defmacro against-my-background [docstring & body]
  `(against-background [(before :contents (setup!)) 
                        (before :contents (data)) 
                        (before :facts (set-access)) 
                        (after :contents (teardown!)]
     (facts ~docstring
       ~@body )))

;; usage
(against-my-background "about this thing i am testing"
  (fact (foo) => :bar)
  (fact (foo) =not=> :baz))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文