 
           server.on 'close', done
 
+        before (done) ->
+          runHookfilesTestAsync done
+
         it 'should modify the transaction with hooks', () ->
-          assert.equal receivedRequest.headers['header'], '123232323'
-          assert.equal receivedRequest.query['key'], 'value'
+          expect(receivedRequest.headers['header']).to.equal('123232323')
+          expect(receivedRequest.query['key']).to.equal('value')
 
         it 'should print message to stdout and stderr', () ->
-          assert.include stdout, 'before-hook-GET-machines'
-          assert.include stderr, 'after-hook-GET-machines'
+          expect(stdout).to.include('before-hook-GET-machines')
+          expect(stderr).to.include('after-hook-GET-machines')
+
+        it 'should exit normally', () ->
+          expect(exitStatus).to.equal(0)
 
 
       describe 'when invoked with "--hooks-only" option', () ->
 
-        before (done) ->
+        runHooksOnlyTestAsync = (done) ->
           ramlFile = "#{RAML_DIR}/single-get.raml"
           cmd = "#{ABAO_BIN} #{ramlFile} --server #{SERVER} --hooks-only"
 
           app = express()
 
           app.get '/machines', (req, res) ->
-            res.setHeader 'Content-Type', 'application/json'
             machine =
               type: 'bulldozer'
               name: 'willy'
-            response = [machine]
-            res.status(200).send response
+            res.status(200).json([machine])
 
           server = app.listen PORT, () ->
             execCommand cmd, () ->


        server.on 'close', done

      it 'exit status should be 0', () ->
        assert.equal exitStatus, 0

      it 'should print count of tests run', () ->
        assert.include stdout, '1 passing'


    describe 'when called with arguments', () ->

      describe 'when invoked with "--reporter" option', () ->

        before (done) ->
          ramlFile = "#{RAML_DIR}/single-get.raml"
          cmd = "#{ABAO_BIN} #{ramlFile} --server #{SERVER} --reporter spec"

          app = express()

          app.get '/machines', (req, res) ->
            res.setHeader 'Content-Type', 'application/json'
            machine =
              type: 'bulldozer'
              name: 'willy'
            response = [machine]
            res.status(200).send response

          server = app.listen PORT, () ->
            execCommand cmd, () ->


          server.on 'close', done

        before (done) ->
          runBaseUriServerTestAsync done

        it 'should print count of tests run', () ->
          expect(report).to.exist
          expect(report).to.have.all.keys(mochaJsonReportKeys)
          expect(report.stats.tests).to.equal(1)
          expect(report.stats.passes).to.equal(1)

        it 'should print correct title for response', () ->
          expect(report.tests).to.have.length(1)
          expect(report.tests[0].fullTitle).to.equal(resTestTitle)

        it 'should exit normally', () ->
          expect(exitStatus).to.equal(0)


    describe 'when executing the command and the server is responding as specified in the RAML', () ->

      resTestTitle = 'GET /machines -> 200 Validate response code and body'

      runNormalTestAsync = (done) ->
        ramlFile = "#{RAML_DIR}/single-get.raml"
        cmd = "#{ABAO_BIN} #{ramlFile} --server #{SERVER} --reporter json"

        app = express()

        app.get '/machines', (req, res) ->
          machine =
            type: 'bulldozer'
            name: 'willy'
          res.status(200).json([machine])

        server = app.listen PORT, () ->
          execCommand cmd, () ->
