Как я могу проверить функцию, которая выдает ожидаемое исключение? Вот функция, которая выдает исключение:
(defn seq-of-maps?
"Tests for a sequence of maps, and throws a custom exception if not."
[s-o-m]
(if-not (seq? s-o-m)
(throw (IllegalArgumentException. "s-o-m is not a sequence"))
(if-not (map? (first s-o-m))
(throw (IllegalArgumentException. "s-o-m is not a sequence of maps."))
true)))
Я хочу разработать тест, подобный следующему, в котором создается и перехватывается исключение, а затем сравнивается. Следующее не работает:
(deftest test-seq-of-maps
(let [map1 {:key1 "val1"}
empv []
s-o-m (list {:key1 "val1"}{:key2 "val2"})
excp1 (try
(seq-of-maps? map1)
(catch Exception e (.getMessage e)))]
(is (seq-of-maps? s-o-m))
(is (not (= excp1 "s-o-m is not a sequence")))))
Я получаю эти ошибки:
Testing util.test.core
FAIL in (test-seq-of-maps) (core.clj:27)
expected: (not (= excp1 "s-o-m is not a sequence"))
actual: (not (not true))
Ran 2 tests containing 6 assertions.
1 failures, 0 errors.
Очевидно, я что-то упустил в написании тестов. Мне трудно это понять. Мой проект был настроен с помощью lein new, и я запускаю тесты с помощью lein test.
Спасибо.