 
         server.on 'close', done
 
-      it 'exit status should be 0', () ->
-        assert.equal exitStatus, 0
+      before (done) ->
+        runRamlIncludesTestAsync done
 
       it 'should print count of tests run', () ->
-        assert.include stdout, '1 passing'
+        expect(stdout).to.have.string('1 passing')
+
+      it 'should exit normally', () ->
+        expect(exitStatus).to.equal(0)
 
 
     describe 'when called with arguments', () ->
 
       describe 'when invoked with "--reporter" option', () ->
 
-        before (done) ->
+        runReporterTestAsync = (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
+            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.equal 1, report.tests.length
          assert.equal 1, report.passes.length

        it 'should print correct title for response', () ->
          assert.equal report.tests[0].fullTitle, 'GET /machines -> 200 Validate response code and body'


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

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

        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) ->
          runHooksOnlyTestAsync done

        it 'should not run test without hooks', () ->
          expect(stdout).to.have.string('1 pending')

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


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

        timeout = undefined
        elapsed = -1
        finished = undefined

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

          beginTime = undefined
          finished = false

          app = express()

          app.use (req, res, next) ->
            beginTime = new Date()
            res.on 'finish', () ->
              finished = true
            next()

          app.use (req, res, next) ->
            delay = timeout * 2
            setTimeout next, delay

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

          server = app.listen PORT, () ->
            execCommand cmd, () ->
              endTime = new Date()
              if finished
                elapsed = endTime - beginTime
                console.log "elapsed = #{elapsed} msecs (req/res)"
              server.close()

          server.on 'close', done


        context 'given insufficient time to complete', () ->

          before (done) ->
            timeout = 20
            console.log "timeout = #{timeout} msecs"
            runTimeoutTestAsync done

          after () ->
            finished = undefined

          it 'should not finish before timeout occurs', () ->
            expect(finished).to.be.false

          # Errors thrown by Mocha show up in stdout; those by Abao in stderr.
          it 'Mocha should throw an error', () ->
            detail = "Error: Timeout of #{timeout}ms exceeded."
            expect(stdout).to.have.string(detail)

          it 'should run test but not complete', () ->
            expect(stdout).to.have.string('1 failing')

          it 'should exit due to error', () ->
            expect(exitStatus).to.equal(1)


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

        runSchemaTestAsync = (done) ->
          pattern = "#{SCHEMA_DIR}/*.json"
          ramlFile = "#{RAML_DIR}/with-json-refs.raml"
          cmd = "#{ABAO_BIN} #{ramlFile} --server #{SERVER} --schemas=#{pattern}"

          app = express()

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

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