Skip to main content

创建模拟对象来使层抽象化

Copilot 对话助手 可帮助创建模拟对象用于单元测试。

创建单元测试时,请务必确保其相互隔离,不依赖于外部服务。 实现这一目标的一种方法是创建模拟对象,以抽象化应用程序的各个层。 Copilot 对话助手 可帮助你生成所需的代码来创建这些模拟对象。

示例方案

假设有一个使用 TypeScript 构建的网站,它显示运行程序列表。 你有一种从数据库提取运行程序的服务,以及使用此服务的服务器端代码。 你想要测试服务器端代码,但不想调用数据库。 可以让 Copilot 对话助手 为此服务生成模拟对象。

// SvelteKit example at +page.server.ts
import service from './service';

export async function load({ params }) {
  const runner = await service.getRunner(params.id);

  return {
    runner
  };
}

示例提示

此示例假定你使用 vitest 进行测试,但也可使用其他测试运行程序。

Copilot Chat prompt
/tests Create a unit test to ensure the service is called correctly. Mock the service object.

如果你现在有一个测试,希望 Copilot 将其作为模型使用,则可以使用 #file: path/to/test-file.js 提供文件路径,并在提示中包含该信息。

示例响应

注意

以下响应是示例。 Copilot 对话助手 的回答是不确定的,因此你可能会得到与这里所显示的不同的回答。

Copilot 提供将要实施的计划和包含模拟的代码文本。

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { load } from './+page.server.ts';

// Mock the entire service module
vi.mock('./service', () => ({
  default: {
    getRunner: vi.fn()
  }
}));

// Import the mocked service
import service from './service';

describe('load function', () => {
  const mockRunner = {
    id: '123',
    name: 'Test Runner',
    status: 'active'
  };

  beforeEach(() => {
    // Reset mock before each test
    vi.resetAllMocks();

    // Setup default mock implementation
    (service.getRunner as any).mockResolvedValue(mockRunner);
  });

  it('should fetch runner with correct id', async () => {
    const params = { id: '123' };
    const result = await load({ params });

    expect(service.getRunner).toHaveBeenCalledWith('123');
    expect(service.getRunner).toHaveBeenCalledTimes(1);
    expect(result).toEqual({ runner: mockRunner });
  });
});

其他阅读材料