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

api*_*iyo 3 testing clojure clojurescript

我写的代码看起来像这样

(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")))))
Run Code Online (Sandbox Code Playgroud)

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

所以我试过这个

(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")))))
Run Code Online (Sandbox Code Playgroud)

似乎没有运行测试。

我也试过这个:

(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])))
Run Code Online (Sandbox Code Playgroud)

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

什么可能导致这种情况?有什么办法可以制作这个测试烘干机吗?我应该尝试使测试干燥吗?

Ala*_*son 5

在 Clojure 中,formap函数都是惰性的,并且在您对输出执行某些操作之前不会运行。

由于您不关心输出,您应该将 the 转换for为 a doseq,它总是立即运行(非懒惰)并且用于像您在这里想要的副作用。

一定要为 Clojure CheatSheet 添加书签并始终在浏览器选项卡中保持打开状态!

这里还列出了许多其他很棒的资源


附录

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

您可以类似地for使用(vec (for ...)). 它像 一样立即运行doseq,但也会返回结果序列(而不仅仅是nil)。您可以在此处查看更多详细信息


cfr*_*ick 5

虽然您的代码未运行的原因已经解决,但我想指出 are

(are argv expr & args)

Checks multiple assertions with a template expression.
See clojure.template/do-template for an explanation of
templates.
 Example: (are [x y] (= x y)  
              2 (+ 1 1)
              4 (* 2 2))
Expands to: 
         (do (is (= 2 (+ 1 1)))
             (is (= 4 (* 2 2))))
 Note: This breaks some reporting features, such as line numbers.
Run Code Online (Sandbox Code Playgroud)

这将允许类似的事情:

(are argv expr & args)

Checks multiple assertions with a template expression.
See clojure.template/do-template for an explanation of
templates.
 Example: (are [x y] (= x y)  
              2 (+ 1 1)
              4 (* 2 2))
Expands to: 
         (do (is (= 2 (+ 1 1)))
             (is (= 4 (* 2 2))))
 Note: This breaks some reporting features, such as line numbers.
Run Code Online (Sandbox Code Playgroud)