{"meta":{"title":"Creating mock objects to abstract layers","intro":"副驾驶聊天 can help with creating mock objects that you can use for unit tests.","product":"GitHub Copilot","breadcrumbs":[{"href":"/zh/copilot","title":"GitHub Copilot"},{"href":"/zh/copilot/tutorials","title":"教程"},{"href":"/zh/copilot/tutorials/copilot-cookbook","title":"GitHub Copilot Cookbook"},{"href":"/zh/copilot/tutorials/copilot-cookbook/testing-code","title":"Testing code"},{"href":"/zh/copilot/tutorials/copilot-cookbook/testing-code/create-mock-objects","title":"Create mock objects"}],"documentType":"article","redirectedFrom":"/zh/copilot/tutorials/copilot-chat-cookbook/testing-code/create-mock-objects"},"body":"# Creating mock objects to abstract layers\n\n副驾驶聊天 can help with creating mock objects that you can use for unit tests.\n\nWhen creating unit tests, it's important to ensure they're isolated and not depending on external services. One way to achieve this is to create mock objects that abstract layers of your application. 副驾驶聊天 can help you generate the code you need to create these mock objects.\n\n## Example scenario\n\nImagine a website built with TypeScript which displays a list of runners. You have a service which fetches the runners from a database, and the server-side code which uses this service. You want to test the server-side code, but you don't want to make a call to the database. You can ask 副驾驶聊天 to generate a mock object for the service.\n\n```typescript id=mock-service\n// SvelteKit example at +page.server.ts\nimport service from './service';\n\nexport async function load({ params }) {\n  const runner = await service.getRunner(params.id);\n\n  return {\n    runner\n  };\n}\n```\n\n## Example prompt\n\nThis example assumes you are using [vitest](https://vitest.dev/) for your tests, but will work with other test runners.\n\n```copilot copy prompt ref=mock-service\n/tests Create a unit test to ensure the service is called correctly. Mock the service object.\n```\n\nIf you have an existing test you wish Copilot to use as a model, you can use `#file: path/to/test-file.js` to provide the path to the file, and include this information in the prompt.\n\n## Example response\n\n> \\[!NOTE] 以下响应是示例。 副驾驶聊天 的回答是不确定的，因此你可能会得到与这里所显示的不同的回答。\n\nCopilot provides the plan it will implement and the code text which includes the mocks.\n\n```typescript\nimport { describe, it, expect, vi, beforeEach } from 'vitest';\nimport { load } from './+page.server.ts';\n\n// Mock the entire service module\nvi.mock('./service', () => ({\n  default: {\n    getRunner: vi.fn()\n  }\n}));\n\n// Import the mocked service\nimport service from './service';\n\ndescribe('load function', () => {\n  const mockRunner = {\n    id: '123',\n    name: 'Test Runner',\n    status: 'active'\n  };\n\n  beforeEach(() => {\n    // Reset mock before each test\n    vi.resetAllMocks();\n\n    // Setup default mock implementation\n    (service.getRunner as any).mockResolvedValue(mockRunner);\n  });\n\n  it('should fetch runner with correct id', async () => {\n    const params = { id: '123' };\n    const result = await load({ params });\n\n    expect(service.getRunner).toHaveBeenCalledWith('123');\n    expect(service.getRunner).toHaveBeenCalledTimes(1);\n    expect(result).toEqual({ runner: mockRunner });\n  });\n});\n```\n\n## Further reading\n\n* [GitHub Copilot 对话助手的提示设计](/zh/copilot/using-github-copilot/prompt-engineering-for-github-copilot)\n* [使用 GitHub Copilot 的最佳做法](/zh/copilot/using-github-copilot/best-practices-for-using-github-copilot)"}