Testing

For testing, Carbon.io comes with a testing framework called Test-Tube. Test-Tube is a generic unit testing framework that comes as part of the Carbon Core.

Carbond extends Test-Tube’s HTTP testing facility to provide an easy way to test your Carbond Services.

Service Tests

The ServiceTest class is an extension of Test Tube’s HttpTest` class that you can use to write declarative HTTP-based unit tests of APIs you make with Carbond.

Consider the following Service:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
var carbon = require('carbon-io')
var o  = carbon.atom.o(module)
var __ = carbon.fibers.__(module)

__(function() {
  module.exports = o.main({
    _type: carbon.carbond.Service,
    port: 8888,
    endpoints: {
      hello: o({
        _type: carbon.carbond.Endpoint,
        get: function(req) {
          return {msg: "Hello World!"}
        },
        post: function(req) {
          return {msg: req.body.msg}
        }
      }) 
    }
  }) 
})

You can test your Service like so:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var carbon = require('carbon-io') 
var __ = carbon.fibers.__(module)
var _o = carbon.bond._o(module)
var carbond = carbon.carbond
var o  = carbon.atom.o(module)

__(function() {
  module.exports = o.main({
    _type: carbond.test.ServiceTest,
    name: "HelloWorldServiceTest",
    service: _o('./HelloWorldService'), // path to your Service
    tests: [
      {
        reqSpec: {
          method: "GET",
          url: '/hello'
        },
        resSpec: {
          statusCode: 200,
          body: {
            msg: "Hello World!"
          }
        }
      },
      {
        reqSpec: {
          method: "POST",
          url: '/hello',
          body: {
            msg: "Hello World!"
          }
        },
        resSpec: {
          statusCode: 200,
          body: {
            msg: "Hello World!"
          }
        }
      }
    ]
  })
})