title: Multiple before functions in HSpec?
body: <p>I have an in-memory repository that I can create by calling this function:</p>
  
  <pre><code>newEmptyRepository :: IO InMemoryGameRepository
  </code></pre>
  
  <p>where <code>InMemoryGameRepository</code> is defined like this:</p>
  
  <pre><code>type State = (HashMap GameId Game)
  type IORefState = IORef State
  newtype InMemoryGameRepository = InMemoryGameRepository IORefState
  </code></pre>
  
  <p>When writing tests for my Scotty application I've seen examples of using this approach:</p>
  
  <pre><code>spec =
    before app $ do
      describe "GET /" $ do
        it "responds with 200" $ get "/" `shouldRespondWith` 200
        it "responds with 'hello'" $ get "/" `shouldRespondWith` "hello"
      ...
  </code></pre>
  
  <p>This is all fine but I need to somehow also initialize the InMemoryGameRepository (by calling <code>newEmptyRepository</code>) and use the created instance in my tests. Thus I've changed <code>app</code> to:</p>
  
  <pre><code>app :: InMemoryGameRepository -&gt; IO Application
  app repo = scottyApp $ routes repo
  </code></pre>
  
  <p>And I'm trying to create a test that uses the repository AND the <code>IO Application</code>, for example like this (which doesn't work):</p>
  
  <pre><code>spec = 
      before (do repo &lt;- newEmptyRepository
                 app repo) $ 
        -- API Tests
        describe "GET /api/games" $ 
          it "responds with " $ do
            liftIO $ startGame repo
            get "/api/games" `shouldRespondWith` singleGameResponse
  </code></pre>
  
  <p>where <code>startGame</code> is defined like this:</p>
  
  <pre><code>startGame :: InMemoryGameRepository -&gt; IO Game
  </code></pre>
  
  <p>Here the compiler says (obviously) that <code>repo</code> is not in scope. But how can I achieve this? I.e. I want to share a single instance of <code>newEmptyRepository</code> both for the <code>app</code> and in the test?</p>
  
  <p>Ps: you can see the full application on <a href="https://github.com/johanhaleby/rps-haskell" rel="nofollow noreferrer">github</a>.</p>