Complete AI Training

Prompt · Software Engineers

Mock Dependency Objects

Use this when you need to create mock objects for external dependencies to simplify unit testing and isolate code behavior.

All 20 prompts in this lesson

How to use it

  1. Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
  2. Replace every {{placeholder}} with your own details, or let the AI ask you for them.
  3. Use the follow-ups below to go deeper.
Prompt

Role You are a senior software engineer specializing in test automation. Your goal is to generate practical, well-structured mock objects that enable isolated and reliable unit testing.

Context you provide

  • {{dependency_type}} — the type of dependency to mock (e.g., database, API, third-party service, file system).
  • {{dependency_name}} — the specific name or identifier of the dependency (e.g., MySQL, Stripe API).
  • {{testing_framework}} — the testing framework and language in use (e.g., Jest for JavaScript, pytest for Python).
  • {{scenarios}} — the specific behaviors to simulate (e.g., success, error, timeout, edge cases).
  • {{code_context}} — optional: relevant code snippets or interfaces to align the mock with.

Instructions

  1. If any required context is missing, ask for it before proceeding.
  2. Based on the dependency type and name, design a mock object that mimics the essential interface and behavior.
  3. Include configurable responses for the specified scenarios, such as success, error, timeout, or custom edge cases.
  4. Provide code examples in the specified language and framework, with comments explaining key parts.
  5. If the dependency has complex behavior, suggest a strategy for simulating it (e.g., using a mocking library or manual stubs).
  6. Ensure the mock is easy to integrate into existing test suites.

Output format A code block with the mock implementation, followed by a brief explanation of how to use it in tests, including setup and teardown considerations. Use clear naming and comments.

Guardrails

  • Do not assume the exact API of the dependency; if unknown, state assumptions and ask for clarification.
  • Keep the mock focused on the specified scenarios; avoid over-engineering.
  • Do not provide production code that bypasses security or error handling.

Example

  • {{dependency_type}} = "database"
  • {{dependency_name}} = "PostgreSQL"
  • {{testing_framework}} = "pytest"
  • {{scenarios}} = "success, connection error, timeout"
  • {{code_context}} = "a repository class with methods get_user(id) and save_user(user)"

Follow-up prompts

  • How can I verify that my mock accurately reflects the real dependency's behavior?
  • What are the best practices for mocking in a microservices architecture?
  • Can you show how to use dependency injection to make mocking easier?