温馨提示:本文翻译自stackoverflow.com,查看原文请点击:testing - tests inside function calls or for statements in clojure not running

testing - 测试函数调用或clojure中的语句未运行

发布于 2020-03-27 15:41:40

我写的代码看起来像这样

(testing "check that for all these roles there's an alert"
    (binding [*profile* account-exceeded-limits]
      (let [overview-page (overview-container sample-form
                                              :role readonly-no-download)]
        (is (dommy/has-class?
             (-> overview-page (sel1 [:div#export-list-panel :.panel-body
                                      :.alert]))
             "alert-warning")))
      (let [overview-page (overview-container sample-form
                                              :role dataentry)]
        (is (dommy/has-class?
             (-> overview-page (sel1 [:div#export-list-panel :.panel-body
                                      :.alert]))
             "alert-warning")))
      (let [overview-page (overview-container sample-form
                                              :role editor)]
        (is (dommy/has-class?
             (-> overview-page (sel1 [:div#export-list-panel :.panel-body
                                      :.alert]))
             "alert-warning")))
      (let [overview-page (overview-container sample-form
                                              :role member)]
        (is (dommy/has-class?
             (-> overview-page (sel1 [:div#export-list-panel :.panel-body
                                      :.alert]))
             "alert-warning")))
      (let [overview-page (overview-container sample-form
                                              :role collaborator)]
        (is (dommy/has-class?
             (-> overview-page (sel1 [:div#export-list-panel :.panel-body
                                      :.alert]))
             "alert-warning")))
      (let [overview-page (overview-container sample-form
                                              :role readonly)]
        (is (dommy/has-class?
             (-> overview-page (sel1 [:div#export-list-panel :.panel-body
                                      :.alert]))
             "alert-warning")))))

我需要重构此代码以使其更干燥。

所以我尝试了这个

(testing "check that for all these roles theres an alert"
    (for [role [dataentry readonly-no-download editor member collaborator
             readonly]]
      (let [overview-page (overview-container sample-form
                                              :role role)]
        (is (dommy/has-class?
             (-> overview-page (sel1 [:div#export-list-panel :.panel-body
                                      :.alert]))
             "alert-warning")))))

似乎没有运行测试。

我也尝试过这个:

(testing "check that for all these roles theres an alert"
(map (fn [role]  (let [overview-page (overview-container sample-form
                                              :role role)]
        (is (dommy/has-class?
             (-> overview-page (sel1 [:div#export-list-panel :.panel-body
                                      :.alert]))
             "alert-warning"))) [dataentry readonly-no-download editor member collaborator
             readonly])))

再次,似乎仍然没有运行测试。

是什么原因造成的?有什么办法可以使我的测试干燥机?我应该尝试使测试干燥吗?

查看更多

查看更多

提问者
apiyo
被浏览
27
Alan Thompson 2020-02-13 06:00

在Clojure中,forand map函数都是惰性的,只有在对输出执行某些操作后才能运行。

由于您不在乎输出,因此应将转换fordoseq,该始终立即运行(非延迟),并用于您想要的副作用。

确保也将Clojure CheatSheet添加为书签并始终在浏览器选项卡中将其保持打开状态!

这里还列出了许多其他重要资源


附录

一个非常有用的表亲mapmapv它只不过是(vec (map ...)),将的输出强制map为(非惰性)向量。在第一次听说Cloclore之前,我已经使用了一段时间。

你同样可以得到一个非延迟版本for使用(vec (for ...))它像一样立即运行doseq,但也返回结果序列(而不是just nil)。您可以在此处查看更多详细信息

发布
问题

分享
好友

手机
浏览

扫码手机浏览