Search code examples
testingclojureexpectations

How to implement fixtures in clojure expectations?


I have recently started using the Clojure test framework expectations. As part of my test, I have a data set that I want to reset to its original value before the start of a new test/expect statement.

In clojure.test, I would just create a fixture and call (use-fixtures :each my-fixture-fn).

I've searched for good examples of how to do this in expectations but haven't had any luck yet. Could anyone provide a concrete example of how to implement a fixture that runs before each test in expectations?


Solution

  • It is possible to use a let statement with expect. Maybe something like this will do the job for you (untested pseudocode):

    (expect 
     (let [dataset {val: "original values"}]
      (function-to-be-tested val:))
     expected-return-value)
    

    This real code works out of the box to show the principle:

    (expect (let [ar [1 2 3]]
       (first ar)) 2)
    

    It returns the following error message as expected:

    failure in (core_test.clj:9) : image-lib.core-test
    (expect (let [ar [1 2 3]] (first ar)) 2)
    
           expected: 1
                was: 2
    
    Ran 2 tests containing 2 assertions in 17 msecs
    1 failures, 0 errors.
    Tests completed at 10:59:45.435