Git Product home page Git Product logo

node-restify-mongoose-testing's Introduction

node-restify-mongoose-testing

Build Status

This repo demonstrates some testing, both unit and integration, of a RESTful api on a node.js server.

The app

  • The api is a simple restify user management app on the node server.
  • Users are stored at a MongoDB and models are handled by the mongoose node module.
  • To provide a quick start of the app, mockgoose is used, so there is no need to setup a real MongoDB.

Just do:

npm install

And:

node server.js

The api

Create a user:

POST /users HTTP/1.1
Host: localhost:3000
Content-Type: application/json

{ "email": "[email protected]", "password": "bbbbbb", "world": "Peasant" }

Response:

{
    "type": true,
    "data": {
        "__v": 0,
        "email": "[email protected]",
        "password": "bbbbbb",
        "world": "Peasant",
        "_id": "560be7bca6581744535d9aa4"
    }
}

Find the user:

GET /users/560be7bca6581744535d9aa4 HTTP/1.1
Host: localhost:3000
Content-Type: application/json

Response:

{
    "type": true,
    "data": {
        "email": "[email protected]",
        "password": "bbbbbb",
        "world": "Peasant",
        "_id": "560be7bca6581744535d9aa4",
        "__v": 0
    }
}

If you create some users in the "Bossland" world you will be able to retrieve them:

POST /users HTTP/1.1
Host: localhost:3000
Content-Type: application/json

{ "email": "[email protected]", "password": "bbbbbb", "world": "Bossland" }
GET /bosses HTTP/1.1
Host: localhost:3000
Content-Type: application/json
{
    "type": true,
    "data": [
        {
            "email": "[email protected]",
            "password": "bbbbbb",
            "world": "Bossland",
            "_id": "560be927a6581744535d9aa5",
            "__v": 0
        }
    ]
}

The specs

Now here comes the interesting part.

  • The spec folder contains both unit and integration tests of the api.
  • Specs are run by mocha. That's solid ground.
  • User unit spec makes use of chai and sinon. Sinon provides some stubs for the models, while chai does a good job regarding assertions.
  • The integration spec adds hippie to the teapot. Hippie can wrap a server, and thus it becomes a good partner for E2E testing and CI, the same way as supertest is.

That said, you can stop the server and run:

npm test

And hopefully you will get an output similar to that:

  Server
    GET /users endpoint
      V returns an existing user
      V returns an error if the id is wrong format
      V returns a not found if the user does not exist
    POST /users endpoint
      V creates a user
      V fails on a 19th century user
    GET /bosses endpoint
      V returns only bosses

  User
    V finds the bosses in Bossland


  7 passing (94ms)

node-restify-mongoose-testing's People

Contributors

eduponte avatar

Watchers

 avatar  avatar

Forkers

alifsn

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.