Image for post

Introduction

Test-driven development (TDD) is an evolutionary approach to development which combines test-first development where you write a test before you write just enough production code to fulfill that test and refactoring. In simple terms, test cases are created before code is written.

In this approach we are using Express as a backend server and mongo-db as a database.

For testing purposes we are using mocha & chais.

Mocha: Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun.

Cha**i:**Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.


Prerequisites

  • Express
  • Mongodb
  • Understanding of rest api
  • Idea for working in backend technology.

Project Setup

Before we start developing our actual API, we have to set up the folder and end point. Create a project folder named as “tddapp ”.

Initializing the project folder to setup the package.json

npm init

In a software project, there is no perfect way to structure an application. Take a look at this GitHub repository for the folder structure followed in this tutorial.


Installing Dependencies


Working on Rest Api

Lets create a models folder inside the root folder of your application. In this tutorial we use models to create the schemas.

_Users.js _:

	const mongoose = require('mongoose')
	const Schema = mongoose.Schema;
	const { userImage } = require('../config/DefaultUserImage');
	const authFunctions = require('../helper/authFunctions');

	const userSchema = new Schema({
	    name: {
	        type: Schema.Types.String,
	        required: true,
	    },
	    email: {
	        type: Schema.Types.String,
	        unique: true
	    },
	    password: {
	        type: Schema.Types.String,
	        required: true
	    },
	    address: {
	        type: Schema.Types.String,
	        required: true,
	    },
	    education_qualification: {
	        type: Schema.Types.String,
	        required: true,
	    },
	    dob: {
	        type: Schema.Types.Date,
	        required: true,
	    },
	    job_title: {
	        type: Schema.Types.String,
	        required: true,
	    },
	    photo: {
	        type: Schema.Types.String,
	        required: false,
	        default: userImage
	    },
	    contact_number: {
	        type: Schema.Types.Number,
	        required: true,
	    },
	}, { timestamps: true })

	const User = mongoose.model('users', userSchema)

	module.exports = {

	    getUsers: () => User.find(),
	    storeUser: async userData => {
	        const hashPassword = await authFunctions.createPassword(userData.password)
	        const user = {
	            name: userData.name,
	            email: userData.email,
	            password: hashPassword,
	            address: userData.address,
	            contact_number: userData.contact_number,
	            education_qualification: userData.education_qualification,
	            dob: userData.dob,
	            job_title: userData.job_title
	        }
	        return new User(user).save();
	    },
	    updateUser: (userData, userId) => User.findByIdAndUpdate(userId, userData),
	    findUserById: userId => User.findById(userId),
	    findUserByEmail: userEmail => User.findOne({ email: userEmail }).exec(),
	    deleteUser: userId => User.findByIdAndRemove(userId),
	    removeUsers: () => User.deleteMany()

	}

#api-testing #apitestinginnode #tddwithnode #mocha #nodejs

TDD(Test Driven Development) Development In Nodejs Using Mongodb & Express.
19.35 GEEK