0

The following code works (obviously):

(use [midje.sweet])

(with-state-changes [(before :contents (println "setup") :after (println "teardown"))]
  (fact "one" 
    (println "doing 1")
    1 => 1)
  (fact "two"
    (println "doing 2")
    (+ 1 1) => 2))

The output is the expected one:

setup
doing 1
doing 2
teardown

But I want to have my facts grouped in a separate function like so:

(defn my-facts []
  (fact "one" ...)
  (fact "two" ...)
  #_( ... ))

(with-state-changes [(before :contents (println "setup") :after (println "teardown"))]
  (my-facts))

This time, midje cannot execute the code, I get the following error:

Midje could not understand something you wrote: 
        Background prerequisites created by the wrapping version of
        `against-background` only affect nested facts. This one
        wraps no facts.

        Note: if you want to supply a background to all checks in a fact, 
        use the non-wrapping form. That is, instead of this:
            (fact 
              (against-background [(f 1) => 1] 
                (g 3 2 1) => 8 
                (h 1 2) => 7)) 
        ... use this:
            (fact 
              (g 3 2 1) => 8 
              (h 1 2) => 7 
              (against-background (f 1) => 1)) 

Is there any way to achieve my goal? I want to use midje's setup- and teardown facilities but still be able to have my facts be contained inside a separate fn.

Sh4pe
  • 1,800
  • 1
  • 14
  • 30

1 Answers1

1

I have spent some time looking over the Midje source code, and I don't think this is possible. Midje relies heavily on parsing and rewriting code during macroexpansion, and by the time it gets the result of your separate function call the Midje macros (fact, etc) will already have been expanded and evaluated. If you are really determined, you could probably make this happen, but it would have to use a macro for the separate facts, not a function, in order to give Midje access to your raw facts forms. Midje is pretty much macros all the way down so it's difficult to interact with using anything else.

Logan Buckley
  • 211
  • 1
  • 4