Learn
Model Testing Patterns
Introduction

A chat app manages messages, users, and chat rooms. A restaurant app manages customers, tables, and orders. How do applications define these entities and their interactions? The answer is the model layer.

Models represent the entities and interactions in a web application’s problem domain: the area of knowledge surrounding a problem. A chat app’s problem domain includes messages, users, and chat rooms; a restaurant’s includes customers, tables, and orders. A model can define each entity, describe the shape of the data stored for each entity, validate the data, store it in a database, and interact with it.

In this lesson, you will learn TDD techniques to develop a model layer using JavaScript with the Mongoose node package and a MongoDB database. For testing, you’ll be using a Mocha test framework and the Chai assertion library.

To better understand the concept of a model, take this example: a full-stack web application manages the inventory of a zoo. It can add animals, remove animals, count animals, and store that information for later use. The app can be divided into three layers:

  • Front-end: a webpage with buttons to allow users to add and remove animals. Could be implemented with HTML and CSS.
  • Server: an application to handle HTTP requests and responses. It routes requests, like the addition and removal of animals, and defines responses, like the count of animals after addition/removal. Could be implemented with Express.
  • Database and Models: storage and shape of the animal data. The data is grouped by animal, each with properties like species name, count, and risk-level. These fields and the methods to interact with them are defined by models, and the storage is managed by a database. Could be implemented with Mongoose models and a MongoDB database.

Just like any other software, you can develop models using Test-Driven Development (TDD). The following exercises will help you write tests specific to the model layer.

Instructions

The zoo for this application keeps a particular type of animal. Get familiar with the data displayed on the right. You will be implementing a model for that data throughout this lesson.

Folder Icon

Sign up to start coding

Already have an account?