Testable

Controllers in KambojaJS is independent piece of code and have least dependencies to KambojaJS internal system. You are free to instantiate Controllers outside KambojaJS system. This behavior make unit testing easy and need minimal use of mock and stub.

import { Controller } from "kamboja-express"

export class UserController extends Controller {
    list(query:string, offset:number, limit:number){
    return this.view({message:query})
    }
}

Inside your test specification you can do this

import { UserController } from "../src/controller/user-controller"
import * as Chai from "chai"

describe("UserController", () => {
    it("Should return model properly", () => {
        let controller = new UserController()
        let result = controller.list("Hello world", 0, 50) 
        Chai.expect(result.model).deep.eq({message:"Hello world!"})
    })
})

When creating Restful API you can throw error to return a specific HTTP status code, that make error handling easy and even easier to test.

import { ApiController, HttpStatusError } from "kamboja-express"
import { MongooseItemModel } from "../model/mongoose-model"

export class ItemsController extends ApiController {
    async get(id:string) { 
        let count = await MongooseItemModel.count({_id: id}).exec()
        if(count == 0) throw new HttpStatusError(404, "Requested item is not found")
        return MongooseItemModel.findById(id).exec()
    }
}

Testing all path of code above is easy, it help you to reach high coverage of unit test.

Inside your test specification you can do this

import { ItemsController } from "../src/controller/items-controller"
import * as Chai from "chai"

describe("ItemsController", () => {
    it("Should return item properly", () => {
        let controller = new ItemsController()
        let result = controller.get(<an exist id>) 
        Chai.expect(result).deep.eq(<the result expected>)
    })

    it("Should throw if requested item not exists", () => {
        let controller = new ItemsController()
        let result =  
        Chai.expect(() => {
            controller.get(<non exist id>)
        }).throw("Requested item is not found")
    })
})

Refer more feature on ApiController reference.