liveBook · Manning

Test fixture

As explained in Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform

A database fixture is a predefined set of data organized for loading into a database to support testing or demonstrations.

A database fixture is a fixed, known set of records loaded into a database for testing. It gives an application predictable data before a test begins. Fixtures can also make a locally running application ready for manual testing or a product demonstration. section 9.7.3 section 10.7.1

Use a fixture when the test needs a real database populated with specific records. A unit test that mocks the MongoDB library can supply fake data through the mock instead. An integration test can insert records directly from its test code; separate fixture files become more useful when loading data for an end-to-end test. section 10.7.1

For a small database-backed test:

  • Start the application and its database before trying to load records. section 4.5.2
  • Choose the collection and records. A Jest helper can reuse the microservice’s existing database connection, drop the test database, and insert the records with insertMany. section 9.7.3
  • Run the test against the seeded state. Keep the database reset confined to testing: dropping the database is not a production operation. section 9.7.3 section 9.8.5

Two videos for a browser test

A two-videos fixture supplies records for the metadata microservice. A Playwright test loads it, visits the application’s root page, and checks that the video list contains two entries with the expected names and links. The known records make those assertions meaningful. section 9.8.5

Listing 10.2 An example database fixture for FlixTube
const { ObjectId } = require("mongodb");                #1
 
module.exports = [                                      #2
  {
    _id: new ObjectId("5ea234a1c34230004592eb32"),      #3
    name: "SampleVideo_1280x720_1mb.mp4"                #4
  },
  {
    _id: new ObjectId("5ea234a5c34230004592eb33"),      #3
    name: "Another video.mp4"                           #4
  }
];

The fixture file exports two video records with fixed MongoDB ObjectIds. Its filename, videos.js, identifies the collection they are loaded into. section 10.7.1

For end-to-end tests, a fixtures REST API can load the data instead of having the browser test connect directly to MongoDB. The test requests a named fixture before checking the running application. section 10.7.1 section 9.8.5

How Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform covers it

How deep it goes
Shows direct insertion through a Jest helper and named loading for Playwright, but does not examine the fixtures REST API implementation.
Examples
Uses a two-record video fixture and a Playwright test that checks the resulting video list.
What it assumes
Assumes a running application and MongoDB database, while defining fixtures and showing how to load their records.
What you can do after
Apply it via a Jest helper or fixture-loading REST API to prepare predictable database state before tests.
How it connects
Connects database fixtures to MongoDB, database mocking, integration testing, end-to-end testing, and REST APIs.

Also explained in

Compare how 10 books explain test fixture →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform. The section links point to the parts of the book it draws on.