Skip to content

Create mock objects to abstract layers #24

@mario-campos

Description

@mario-campos

When 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. Copilot Chat can help you generate the code you need to create these mock objects.

Example scenario

Imagine 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 Copilot Chat to generate a mock object for the service.

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

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

  return {
    runner
  };
}

Example prompt

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

Source: https://docs.github.com/en/copilot/copilot-chat-cookbook/testing-code/create-mock-objects-to-abstract-layers

Metadata

Metadata

Assignees

No one assigned

    Labels

    SvelteExercise involves the SvelteKit frameworkTypeScriptExercise includes TypeScript code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions