Skip to content
ios-generate-unit-tests logo

iOS RxSwift Unit Test Generator

ios-generate-unit-tests

Generate comprehensive unit tests for iOS RxSwift project files (ViewModels, UseCases, Services). Creates complete test files with nested mocks, RxTest patterns, session error handling, and memory leak tests. Use when "generate unit tests", "write tests for", "create unit tests", "add tests for",...

SKILL.md

Full skill instructions

iOS RxSwift Unit Test Generator

Automatically generate comprehensive unit tests for iOS RxSwift project following Clean Architecture patterns.

When to Activate

  • "generate unit tests for [file]"
  • "write tests for [ViewModel/​UseCase/​Service]"
  • "create unit tests for this file"
  • "add tests for [class name]"
  • "test [file path]"

Process

1. Identify Target File

Ask user for file to test (if not provided):

Which file would you like to generate tests for?
Provide the file path or class name (e.g., BalanceInformationViewModel.swift)

Read target file and determine type:

  • ViewModel: Has ViewModelType, Input, Output, transform method
  • UseCase: Implements *UseCaseType protocol
  • Service: Implements service protocol

2. Read Testing Guide

CRITICAL: Read the comprehensive testing guide:

~/​Library/​Application Support/​Code/​User/​prompts/​ios-mc-generate-unit-test.prompt.md

This guide contains ALL rules, patterns, and examples to follow.

3. Analyze Target File

Extract from target file:

  • Class name and type (ViewModel/​UseCase/​Service)
  • All dependencies (injected in initializer)
  • Protocol types for each dependency
  • Public methods and their signatures
  • Input/​Output structure (for ViewModels)
  • Observable/​Single/​Completable return types

Search for protocol definitions if needed:

Glob: **/​*UseCaseType.swift
Grep: "protocol [DependencyName]"

4. Generate Test File

Create complete test file at:

PayooMerchantTests/[appropriate-folder]/[ClassName]Tests.swift

Folder structure:

  • ViewModels → PayooMerchantTests/​ViewModel/
  • UseCases → PayooMerchantTests/​UseCase/
  • Services → PayooMerchantTests/​Mock/​Service/

Test file MUST include:

  1. Required imports:

    import XCTest
    import RxSwift
    import RxCocoa
    import RxTest
    import RxBlocking
    import Domain
    @testable import PayooMerchant
    
  2. Test class with nested mocks:

    • ALL mocks as private final class nested inside test class
    • One mock for EACH dependency
    • Include call tracking (callCount, lastParams)
    • Include configurable return values
  3. Properties section:

    • private var disposeBag: DisposeBag!
    • private var scheduler: TestScheduler!
    • Mock instances for each dependency
  4. setUp/​tearDown:

    • Initialize disposeBag, scheduler, all mocks
    • Clean up all properties to nil
  5. Test data factories:

    • Helper methods to create test entities
    • Use default parameters
  6. Comprehensive test methods:

    • Initial load success
    • Load error handling
    • Empty state
    • Loading state
    • Session error handling (if API calls)
    • Memory leak test (for ViewModels)
    • Permission tests (if applicable)
    • User action tests
    • State transition tests
    • Edge cases

Follow naming convention:

func test_methodName_condition_expectedBehavior()

5. Generate Tests Based on Type

For ViewModels:
  • Test each Input → Output transformation
  • Use TestScheduler and hot observables for inputs
  • Create observers for each output driver
  • Test loading states, errors, empty states
  • Include memory leak test:
    func test_viewModel_shouldDeallocateProperly()
    
For UseCases:
  • Test each public method
  • Mock all service dependencies
  • Test success and error scenarios
  • CRITICAL: Test session error handling:
    func test_catchSessionError_SessionTimeoutError_shouldSetExpiredState()
    func test_catchSessionError_ForceUpdateError_shouldSetForceUpdateState()
    
For Services:
  • Test all CRUD operations
  • Test observable streams
  • Test data persistence

6. Validate Generated Tests

Ensure:

  • ✅ All mocks are nested private final class
  • ✅ Proper imports included
  • ✅ setUp/​tearDown with cleanup
  • ✅ DisposeBag and TestScheduler used
  • ✅ Descriptive test names
  • ✅ Descriptive assertions with messages
  • ✅ All dependencies mocked
  • ✅ Session error tests for API calls
  • ✅ Memory leak test for ViewModels

Output Format

After generating tests, show:

✅ Generated Unit Tests: [ClassName]Tests.swift

📁 Location: PayooMerchantTests/[folder]/[ClassName]Tests.swift

📊 Test Coverage:
- [X] Nested mocks created: [count]
- [X] Test methods: [count]
- [X] Scenarios covered:
  ✓ Success cases
  ✓ Error handling
  ✓ Empty states
  ✓ Loading states
  ✓ Session errors (if API)
  ✓ Memory leak test (if ViewModel)
  ✓ [Other scenarios]

🎯 Test Naming Pattern:
test_methodName_condition_expectedBehavior

⚡ Next Steps:
1. Review generated tests
2. Run: xcodebuild test -scheme PayooMerchantTests
3. Or run specific plan: bundle exec fastlane run_test_plan test_plan:"[plan-name]"

📖 Generated following guide: ~/​Library/​Application Support/​Code/​User/​prompts/​ios-mc-generate-unit-test.prompt.md

Key Rules (from Testing Guide)

  1. ALWAYS create mocks as nested private final class
  2. ALWAYS use TestScheduler for ViewModels
  3. ALWAYS include session error tests for API calls
  4. ALWAYS include memory leak test for ViewModels
  5. ALWAYS use descriptive assertions with messages
  6. ALWAYS follow Arrange-Act-Assert pattern
  7. NEVER create global/​standalone mock classes
  8. NEVER skip setUp/​tearDown cleanup

Example Test Structure

final class BalanceInformationViewModelTests: XCTestCase {
    // MARK: - Mocks
    private final class MockBalanceUseCase: BalanceUseCaseType {
        var getBalanceResult: Single<Balance> = .never()
        var getBalanceCallCount = 0
        
        func getBalance() -> Single<Balance> {
            getBalanceCallCount += 1
            return getBalanceResult
        }
    }
    
    // MARK: - Properties
    private var disposeBag: DisposeBag!
    private var scheduler: TestScheduler!
    private var mockBalanceUC: MockBalanceUseCase!
    
    // MARK: - Setup & Teardown
    override func setUp() {
        super.setUp()
        disposeBag = DisposeBag()
        scheduler = TestScheduler(initialClock: 0)
        mockBalanceUC = MockBalanceUseCase()
    }
    
    override func tearDown() {
        disposeBag = nil
        scheduler = nil
        mockBalanceUC = nil
        super.tearDown()
    }
    
    // MARK: - Test Data Factory
    private func makeTestBalance() -> Balance {
        // ...
    }
    
    // MARK: - Tests
    func test_transform_withLoadTrigger_shouldReturnBalance() {
        // Arrange
        // Act
        // Assert
    }
}

References:

  • Testing Guide: ~/​Library/​Application Support/​Code/​User/​prompts/​ios-mc-generate-unit-test.prompt.md
  • Project Structure: PayooMerchantTests/
  • Existing Tests: Use as reference for patterns

More skills from majiayu000

xiaohongshu logo
majiayu000/claude-arsenal

xiaohongshu

xiaohongshu

286 148
View
agent-task-conductor logo
majiayu000/claude-skill-registry

agent-task-conductor

Conduct multi-agent task orchestration and workflow coordination.

663 1
View
conductor-setup logo
majiayu000/claude-skill-registry

conductor-setup

Initialize project with Conductor artifacts (product definition,

663 1
View
animation-designer logo
majiayu000/claude-skill-registry

animation-designer

Expert in web animations, transitions, and motion design using Framer Motion and CSS

663 1
View
diagramming logo
majiayu000/claude-skill-registry

diagramming

Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...

663 1
View
h3-pg logo
majiayu000/claude-skill-registry-data

h3-pg

PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.

23 1
View
conductor-development logo
majiayu000/claude-skill-registry

conductor-development

Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...

663 1
View
conductor-status logo
majiayu000/claude-skill-registry

conductor-status

Display project status, active tracks, and next actions

663 1
View
dockerization logo
majiayu000/claude-skill-registry

dockerization

Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.

663 1
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View