2

I have a function defined as:

(defn strict-get
  [m key]
  {:pre [(is (contains? m key))]}
  (get m key))

And then I have a test for it:

(is (thrown? java.lang.AssertionError (strict-get {} :abc)))

However this test fails:

  ;; FAIL in () (myfile.clj:189)
  ;; throws exception when key is not present
  ;; expected: (contains? m key)
  ;; actual: (not (contains? {} :abc))

What is needed to check that the assertion would throw an error?

nha
  • 17,623
  • 13
  • 87
  • 133
  • 2
    Isn't there redundant `is` in the body of the precondition? try change it to `{:pre [(contains? m key)]}` – OlegTheCat Nov 30 '16 at 20:45
  • @OlegTheCat well spotted! I am using this trick from there: http://stackoverflow.com/a/24836592/1327651 I guess I have to choose between tests and useful REPL errors. – nha Nov 30 '16 at 21:42
  • 1
    From Clojure 1.9 you don't have to chose. See http://stackoverflow.com/questions/37885542/meaningful-error-message-for-clojure-spec-validation-in-pre – NielsK Dec 01 '16 at 10:22

1 Answers1

3

The reason your assertion fails because you are nesting two is. The inner is already catches the exception so the outer is test then fails because nothing is thrown.

(defn strict-get
  [m key]
  {:pre [(contains? m key)]} ;; <-- fix
  (get m key))

(is (thrown? java.lang.AssertionError (strict-get {} nil)))
;; does not throw, but returns exception object for reasons idk

(deftest strict-get-test
  (is (thrown? java.lang.AssertionError (strict-get {} nil))))

(strict-get-test) ;; passes
Leon Grapenthin
  • 9,246
  • 24
  • 37