viewmodel-testing-expert

Unit test Android ViewModels with Turbine and MainDispatcherRule.

Updated Jan 11, 2025
One-click install
npx skills add https://github.com/shaharKeisarApps/MoviesAndBeyond --skill viewmodel-testing-expert
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: viewmodel-testing-expert
Source: https://github.com/shaharKeisarApps/MoviesAndBeyond/tree/main/.claude/skills/viewmodel-testing-expert
Command: npx skills add https://github.com/shaharKeisarApps/MoviesAndBeyond --skill viewmodel-testing-expert

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This skill provides a structured approach to unit testing Android ViewModels annotated with @HiltViewModel by leveraging Turbine for StateFlow testing, MainDispatcherRule for dispatcher control, and test doubles to simulate repository data.

Core Features & Use Cases

  • Test StateFlow emissions: Validate loading, data, and error states in ViewModels using Turbine.
  • Dispatcher management: Use MainDispatcherRule (or StandardTestDispatcher) to control coroutines during tests.
  • Mock data sources: Drive repository responses with a FakeContentRepository to simulate success, loading, and error paths.
  • Pagination and caching patterns: Verify accumulative data, refresh resets, and isFromCache indicators.
  • Error handling and UX flows: Confirm error messages, and that errors can be dismissed via onErrorShown.

Quick Start

  • Create a test class for your HiltViewModel, apply MainDispatcherRule, and write a Turbine-based test asserting StateFlow transitions using a FakeContentRepository.

Frequently Asked Questions about viewmodel-testing-expert

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I unit test Android ViewModel StateFlow emissions with Turbine?

You can test Android ViewModel StateFlow emissions with Turbine by applying MainDispatcherRule to control coroutines and using a FakeContentRepository to drive and assert loading, data, and error state transitions. It validates repository-driven flows including pagination and caching.

What is the best way to test loading and error states in Kotlin coroutines ViewModels?

The best way to test loading and error states in Kotlin coroutines ViewModels is using Turbine to collect StateFlow emissions and a FakeContentRepository to simulate success, loading, and error paths. This approach confirms error messages and verifies that errors can be dismissed via onErrorShown.

Does ViewModel testing with Turbine require a MainDispatcherRule for coroutine control?

Yes, ViewModel testing with Turbine requires a MainDispatcherRule or StandardTestDispatcher to control coroutines during tests. This dispatcher setup ensures deterministic testing of StateFlow transitions and repository-driven flows in your HiltViewModel unit tests.

How do I verify pagination and caching patterns in Android ViewModel unit tests?

You verify pagination and caching patterns in Android ViewModel unit tests by using a FakeContentRepository to drive observed flows and Turbine to assert accumulative data, refresh resets, and isFromCache indicators. This validates StateFlow emissions for repository-driven flows.

Can I use test doubles to simulate repository data for HiltViewModel unit testing?

Yes, you can use test doubles like a FakeContentRepository to simulate repository data for HiltViewModel unit testing. This allows you to drive observed flows and test success, loading, and error paths in your ViewModels using Turbine and MainDispatcherRule.