Skip to content

Integration 测试使用真实数据库

中等 约 45 分钟

你将构建什么

用户仓库模块那个 interacts 使用数据库, 使用 integration 测试 using in-memory 数据库到 验证完整数据 flow.

学习目标

  • 创建仓库模式使用 CRUD 操作
  • Use in-memory 数据库 for integration 测试
  • 设置 and tear 下数据库之间测试
  • 测试完整数据 flow 从仓库到 storage
  • Distinguish 单元测试从 integration 测试

前置要求

  • 已完成项目 1 and 2 or equivalent experience
  • 理解的 CRUD 操作
  • 基础 knowledge 的仓库模式

步骤

  1. 1

    创建 in-memory 数据库

    创建简单 in-memory 数据库模块 for 测试.

    代码
    // inMemoryDb.js
    class InMemoryDb {
      constructor() { this.data = new Map(); }
      insert(id, record) { this.data.set(id, record); return record; }
      findById(id) { return this.data.get(id); }
      update(id, record) { this.data.set(id, record); return record; }
      delete(id) { return this.data.delete(id); }
      clear() { this.data.clear(); }
    }
    module.exports = { InMemoryDb };

    预期结果

    In-memory 数据库是 创建.

  2. 2

    创建用户仓库

    创建 UserRepository 那个 uses 数据库 for CRUD 操作.

    代码
    class UserRepository {
      constructor(db) { this.db = db; }
      create(user) { return this.db.insert(user.id, user); }
      findById(id) { return this.db.findById(id); }
      update(id, updates) {
        const existing = this.db.findById(id);
        if (!existing) throw new Error('User not found');
        return this.db.update(id, { ...existing, ...updates });
      }
      delete(id) { return this.db.delete(id); }
    }
    module.exports = { UserRepository };

    预期结果

    UserRepository 是创建.

  3. 3

    编写 integration 测试

    创建 integration 测试那个 use 真实 in-memory 数据库, 使用设置/teardown 到 reset 状态.

    代码
    const { InMemoryDb } = require('./inMemoryDb');
    const { UserRepository } = require('./userRepository');
    
    describe('UserRepository integration', () => {
      let db, repo;
      beforeEach(() => { db = new InMemoryDb(); repo = new UserRepository(db); });
    
      test('create and find user', () => {
        repo.create({ id: 1, name: 'Alice' });
        expect(repo.findById(1)).toEqual({ id: 1, name: 'Alice' });
      });
    
      test('update user', () => {
        repo.create({ id: 1, name: 'Alice' });
        repo.update(1, { name: 'Bob' });
        expect(repo.findById(1).name).toBe('Bob');
      });
    });

    预期结果

    所有 integration 测试通过.

  4. 4

    测试错误场景

    添加测试 for 更新 and 删除非-existent 用户.

    代码
    test('throws when updating non-existent user', () => {
      expect(() => repo.update(999, { name: 'X' })).toThrow('User not found');
    });

    预期结果

    错误测试通过.

  5. 5

    添加边界测试用例

    为测试套件添加边界值用例:空数组、零值、最大值、null/undefined。边界用例是最容易出错的地方,必须显式覆盖。

    代码
    describe("boundary cases", () => {
      it("handles empty array", () => {
        expect(func([])).toEqual([]);
      });
    
      it("handles zero", () => {
        expect(func(0)).toBe(0);
      });
    
      it("handles null", () => {
        expect(() => func(null)).toThrow();
      });
    
      it("handles max value", () => {
        expect(func(Number.MAX_SAFE_INTEGER)).toBeDefined();
      });
    });

    预期结果

    所有边界用例通过,无未捕获异常。

自己动手试试

你学到了什么

你学习 difference 之间单元测试 (mocked dependencies) and integration 测试 (真实 dependencies). Integration 测试验证那个组件工作 together 正确地, catching issues 如数据格式 mismatches 那个单元测试使用 mocks 可能 miss.

相关资源

相关课程

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回主题