Skip to content

Setup, Teardown, and State

marick edited this page Dec 9, 2010 · 11 revisions

This description is valid for Midje 0.8.1 and following.

The short version

Read thing-wrapped as :checks, :facts, or :contents. Note these are all keys. They're explained below.

A wrapper is one of the following:

  • (around thing-wrapped (... ?form ...))

    Whenever the thing-wrapped is seen, it is substituted for the ?form. Note the leading ? --- it's required.

  • (before thing-wrapped (... code ...) :after (... code ...))

    The two code blocks are executed before and after the thing-wrapped. The :after and what follows can be omitted. The :after code is executed even if the thing-wrapped throws an exception.

  • (after thing-wrapped (... code ...))

    The code block is executed after the thing-wrapped. Nothing's done before it.

Zero or more wrappers can be found in three different forms:

  • (against-background [ wrappers ] ...) The wrappers apply to all the forms within the against-background.

  • (fact ... (against-background wrappers ) ...) Semantically, this is the same as an against-background that wraps this single fact. It's convenient if you use magical keypresses to send a whole fact to a REPL to be evaluated. (But see also background below.) The against-background form can appear anywhere in a facts top-level forms, and there can be more than one. Note that it's not surrounded by [].

  • (background wrappers ...) The wrappers take effect immediately and apply until they're erased by another background.

The following shows what different things-wrapped means:

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

The longer version

The following defines setup for each fact that follows it in the file. An atom is initialized to zero, and a database connection is made available in a local variable.

 (background (start :facts (swap! my-atom (constantly 0)))
                       (around :facts (sql/with-connection db ?form)))

The same can be done to a selected group of facts by wrapping them with against-background:

 (against-background (start :facts (swap! my-atom (constantly 0)))
                                    (around :facts (sql/with-connection db ?form)))
     (fact...)
     (fact ....)
Clone this wiki locally