marick/Midje

How do I execute a function before Midje `fact` calls only? There’s (before :facts) but it executes before `facts` calls too

Closed this issue · 2 comments

zignd commented

My unit tests require certain setup before every fact. It seems that the only option available that would let me define a similar behavior would be a call to before passing :facts, but it executes before a facts call and before every nested fact call and I need something that would execute exclusively before fact calls.

Here's an example that might help describe:

(facts "add-something"
	(background (before :facts (println "before") :after (println "after")))
	(fact "should add when there's something to add"
		(println "1"))
	(fact "should fail to add when there's nothing to add"
		(println "2")))

Output:

before ; i'd like to skip the call generating this output
before
1
after
before
2
after
after ; and this last one too

But I'd like to skip the first call to (println "before") and the last call to (println "after").

You want to use :contents (with either before and after or around). See https://github.com/marick/Midje/wiki/Deprecated-setup-and-teardown

:contents is a lousy word, but I couldn't think of a better one. The whole mechanism is iffy.

Here's an example from that page:

     (against-background [ (before :contents (A)) (before :facts (B)) (before :checks (C)) ]
        ; A is evaluated here.
        ; B is evaluated here
        (fact 
            ; C is evaluated here
            (f) => 1
            ; C is evaluated here
            (g) => 2)
       
        ; B is evaluated here
        (fact 
           ; C is evaluated here
           (f) => 1))
zignd commented

Thanks @marick, it worked as you described!