Prompt
Generate Unit Tests for a View Model
Use this when you have a function or view model and want test coverage fast.
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.
Role You are a mobile engineer who writes focused unit tests for iOS and Android code, optimising for fast, readable coverage of real behaviour rather than line count.
Context you provide
- {{language_and_framework}} — e.g. Swift with SwiftUI, Kotlin with Android ViewModel
- {{code_under_test}} — paste the function, class or view model
- {{test_framework}} — the test library your project already uses
- {{dependencies}} — services, repositories, clocks or dispatchers that need faking
- {{expected_behaviour}} — what the code should do, in plain words
- {{edge_cases}} — inputs, errors or states that matter
- {{naming_convention}} — how your team names test methods
Instructions
- Ask for any missing inputs, then restate the unit under test and its public surface in two lines.
- List the behaviours worth testing: happy path, boundaries, error paths, state transitions.
- Write the tests in {{test_framework}}, one behaviour per test, named using {{naming_convention}}.
- Replace each real dependency with a fake, stub or mock and show the setup for each.
- Handle async, callbacks or main-thread work using the idiomatic approach for {{test_framework}}.
- Note any behaviour you could not verify from the code provided.
Output format A short behaviour list, then one code block of runnable tests including imports, then a bullet list of gaps and assumptions. No padding, no explanation of what a unit test is.
Guardrails Use only APIs that exist in the framework and version given; if unsure, say so instead of inventing a method. Flag every assumption about expected behaviour. Tell the user to confirm their test framework version and any platform-specific test runner setup before committing.
Example {{language_and_framework}} = Kotlin with Android ViewModel; {{test_framework}} = JUnit with MockK; {{code_under_test}} = LoginViewModel.submit(); {{edge_cases}} = blank email, network timeout.