 
           server.on 'close', done
 
-        it 'exit status should be 0', () ->
-          assert.equal exitStatus, 0
-
-
-      describe 'when invoked with "--schema" option and expecting error', () ->
-
-        before (done) ->
-          ramlFile = "#{RAML_DIR}/with-json-refs.raml"
-          cmd = "#{ABAO_BIN} #{ramlFile} --server #{SERVER} --schemas=#{SCHEMA_DIR}/*.json"
-
-          app = express()
-
-          app.get '/machines', (req, res) ->
-            res.setHeader 'Content-Type', 'application/json'
-            machine =
-              typO: 'bulldozer'
-              name: 'willy'
-            response = [machine]
-            res.status(200).send response
-
-          server = app.listen PORT, () ->
-            execCommand cmd, () ->
-              server.close()
-
-          server.on 'close', done
-
-        it 'exit status should be 1', () ->
-          assert.equal exitStatus, 1
-
+        before (done) ->
+          runSchemaTestAsync done
+
+        it 'should exit normally', () ->
+          expect(exitStatus).to.equal(0)
+
+
+        describe 'when expecting error', () ->
+
+          runSchemaFailTestAsync = (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 =
+                typO: 'bulldozer'       # 'type' != 'typ0'
+                name: 'willy'
+              res.status(200).json([machine])
+
+            server = app.listen PORT, () ->
+              execCommand cmd, () ->
+                server.close()
+
+            server.on 'close', done
+
+          before (done) ->
+            runSchemaFailTestAsync done
+
+          it 'should exit due to error', () ->
+            expect(exitStatus).to.equal(1)
+

  report = ''
  exitStatus = null

  cli = exec CMD_PREFIX + cmd, (error, out, err) ->
    stdout = out
    stderr = err
    try
      report = JSON.parse out

    if error
      exitStatus = error.code

  cli.on 'close', (code) ->
    exitStatus = code if exitStatus == null and code != undefined
    callback()




      describe 'when RAML file does not specify "baseUri"', () ->

        runUnspecifiedServerTestAsync = (done) ->
          ramlFile = "#{RAML_DIR}/no-base-uri.raml"
          cmd = "#{ABAO_BIN} #{ramlFile} --reporter json"

          execCommand cmd, done

        before (done) ->
          runUnspecifiedServerTestAsync done

        it 'should print error message to stderr', () ->
          expect(stderr).to.include('no API endpoint specified')

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


      describe 'when RAML file specifies "baseUri"', () ->

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

        runBaseUriServerTestAsync = (done) ->
          ramlFile = "#{RAML_DIR}/single-get.raml"
          cmd = "#{ABAO_BIN} #{ramlFile} --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, () ->
