With-state-changes and function calls

22 views
Skip to first unread message

David Nies

unread,
Jul 6, 2015, 9:40:26 AM7/6/15
to mi...@googlegroups.com

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.

Brian Marick

unread,
Jul 7, 2015, 3:25:54 PM7/7/15
to mi...@googlegroups.com
`with-state-changes` is a macro. Part of its expansion is that it checks
to see whether there are any actual facts inside it. (I think I did that
in response to someone's confusion - naturally, it would result in
someone else's confusion.)

You could put a dummy fact - just `(fact)` - inside the
`with-state-changes` to fake it out.

Or you could put the `with-state-changes` inside your function definition.

It's unusual to put facts inside of a function. If you reply describing
what you're wanting Midje to do for you, I might be able to explain a
different way.

All top-level facts are actually expanded and stored as functions, as
explained here: https://github.com/marick/Midje/wiki/The-compendium
You can fetch and run them programmatically. I confess I never any
longer use that. I just run Midje in a repl, and use autotest to rerun
namespaces full of tests whenever needed.



David Nies wrote:
> 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
> doing1
> doing2
> 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 bythe 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,
> usethe non-wrapping form. That is, instead of this:
> (fact
> (against-background[(f1) => 1]
> (g3 2 1) => 8
> (h1 2) => 7))
> ... use this:
> (fact
> (g3 2 1) => 8
> (h1 2) => 7
> (against-background(f1) => 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|.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Midje" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to midje+un...@googlegroups.com
> <mailto:midje+un...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages