pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://docs.github.com/ko/copilot/tutorials/copilot-cookbook/testing-code/create-mock-objects

content="ko" data-next-head=""/>
Skip to main content

레이어를 추상화하기 위한 모의 개체 만들기

공동 파일럿 채팅 단위 테스트에 사용할 수 있는 모의 개체를 만드는 데 도움이 될 수 있습니다.

단위 테스트를 만들 때는 테스트가 격리되어 있고 외부 서비스에 의존하지 않도록 하는 것이 중요합니다. 이를 구현하는 한 가지 방법은 애플리케이션의 레이어를 추상화하는 모의 개체를 만드는 것입니다. 공동 파일럿 채팅 는 이러한 모의 개체를 만드는 데 필요한 코드를 생성하는 데 도움이 될 수 있습니다.

예제 시나리오

TypeScript로 빌드되어 실행기 목록을 표시하는 웹사이트를 가정해 보세요. 데이터베이스에서 실행기를 가져오는 서비스와 이 서비스를 사용하는 서버 쪽 코드가 있다고 가정합니다. 서버 쪽 코드를 테스트하고 싶지만, 데이터베이스 호출은 수행하고 싶지 않습니다. 서비스에 대한 모의 개체를 생성하도록 요청할 공동 파일럿 채팅 수 있습니다.

// 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 prompt
/tests Create a unit test to ensure the service is called correctly. Mock the service object.

모델로 사용하려는 기존 테스트가 있는 경우, 파일 경로를 제공하기 위해 #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 });
  });
});

추가 읽기

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy