Skip to main content

๋ ˆ์ด์–ด๋ฅผ ์ถ”์ƒํ™”ํ•˜๊ธฐ ์œ„ํ•œ ๋ชจ์˜ ๊ฐœ์ฒด ๋งŒ๋“ค๊ธฐ

Copilot Chat์€ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ์˜ ๊ฐœ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค ๋•Œ ์™ธ๋ถ€ ์„œ๋น„์Šค์— ๋”ฐ๋ผ ๊ฒฉ๋ฆฌ๋˜์ง€ ์•Š๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๋‹ฌ์„ฑํ•˜๋Š” ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ณ„์ธต์„ ์ถ”์ƒํ™”ํ•˜๋Š” ๋ชจ์˜ ๊ฐœ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. Copilot Chat์€ ์ด๋Ÿฌํ•œ ๋ชจ์˜ ๊ฐœ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ํ•„์š”ํ•œ ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ ์‹œ๋‚˜๋ฆฌ์˜ค

์‹คํ–‰๊ธฐ ๋ชฉ๋ก์„ ํ‘œ์‹œํ•˜๋Š” TypeScript๋กœ ๋นŒ๋“œ๋œ ์›น ์‚ฌ์ดํŠธ๋ฅผ ์ƒ์ƒํ•ด ๋ณด์„ธ์š”. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์‹คํ–‰๊ธฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์„œ๋น„์Šค์™€ ์ด ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ฒ„ ์ชฝ ์ฝ”๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์„œ๋ฒ„ ์ชฝ ์ฝ”๋“œ๋ฅผ ํ…Œ์ŠคํŠธํ•˜๋ ค๊ณ  ํ•˜์ง€๋งŒ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์‹ถ์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. Copilot Chat์—๊ฒŒ ์„œ๋น„์Šค์— ๋Œ€ํ•œ ๋ชจ์˜ ๊ฐœ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋„๋ก ์š”์ฒญํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

// 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๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ํ…Œ์ŠคํŠธ ์‹คํ–‰๊ธฐ์—์„œ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

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

Copilot๋ฅผ ๋ชจ๋ธ๋กœ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ธฐ์กด ํ…Œ์ŠคํŠธ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ #file: path/to/test-file.js๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ œ๊ณตํ•˜๊ณ  ํ”„๋กฌํ”„ํŠธ์— ์ด ์ •๋ณด๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ ์‘๋‹ต

์ฐธ๊ณ  ํ•ญ๋ชฉ

๋‹ค์Œ ์‘๋‹ต์ด ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. Copilot Chat ์‘๋‹ต์€ ๋น„๊ฒฐ์ •์ ์ด๋ฏ€๋กœ ์—ฌ๊ธฐ์— ํ‘œ์‹œ๋œ ์‘๋‹ต๊ณผ ๋‹ค๋ฅธ ์‘๋‹ต์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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 });
  });
});

์ถ”๊ฐ€ ์ฐธ๊ณ  ์ž๋ฃŒ