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.
How to use it
- Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
- Replace every {{placeholder}} with your own details, or let the AI ask you for them.
- 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
- If any required context is missing, ask for it before proceeding.
- Based on the dependency type and name, design a mock object that mimics the essential interface and behavior.
- Include configurable responses for the specified scenarios, such as success, error, timeout, or custom edge cases.
- Provide code examples in the specified language and framework, with comments explaining key parts.
- If the dependency has complex behavior, suggest a strategy for simulating it (e.g., using a mocking library or manual stubs).
- 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)andsave_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?