Mock call count retry fallback

From divinevideo/divine-mobile · View interactive version

Mock call count retry fallback is an AI agent skill from the GitHub repository divinevideo/divine-mobile. Fix test failures when adding retry/fallback logic causes mock call count mismatches. Use when: (1) Test fails with "Expected: <1>, Actual: <2>" or similar call count errors after adding retry or fallback mechanisms, (2) MissingStubError appears for methods in new fallback code paths, (3) Mockito ve

How to use this skill

Skill file

Mock Call Count Failures After Adding Retry/Fallback Logic

Problem

When you add retry logic, fallback mechanisms, or alternative code paths to a service,

existing tests that verify exact mock method call counts will fail. The tests were

written for the original behavior and don't account for the new retry/fallback calls.

Context / Trigger Conditions

Solution

Step 1: Identify the New Code Paths

Trace through your new retry/fallback logic to understand:

Step 2: Mock New Dependencies

If your fallback code calls methods that weren't previously mocked:

// BEFORE: Only mocked the primary method
when(mockService.primaryMethod(any)).thenAnswer((_) async => 'result');

// AFTER: Also mock fallback-related methods
when(mockService.primaryMethod(any)).thenAnswer((_) async => 'result');
when(mockService.addFallbackServer(any)).thenAnswer((_) async => false);  // Graceful failure
when(mockService.queryFallback(any)).thenAnswer((_) async => []);

Step 3: Update Expected Call Counts

// BEFORE: Expected single call
verify(mockService.createSubscription(...)).called(1);

// AFTER: Expected calls = initial + retries
// Document WHY the count changed
verify(mockService.createSubscription(...)).called(2);  // initial + retry after fallback

Step 4: Add Comments Explaining the Count

// Verify subscription was created
// Note: With the indexer fallback logic, createSubscription is called twice:
// 1. First attempt via main relay batch fetch
// 2. Retry attempt after indexer fallback fails
verify(
  mockService.createSubscription(...),
).called(2);

Verification

  1. Run the specific test: flutter test --name "test name"
  2. Verify the test passes
  3. Verify the service's actual behavior matches the expected retry count

Example

Scenario: Added indexer relay fallback to profile fetching

Original behavior:

New behavior:

  1. Fetch profile from main relay (subscription #1)
  2. If not found, try indexer relays
  3. If indexers fail, retry main relay (subscription #2)
  4. After 2 failures, mark profile as missing

Test fix:

test('should force refresh profile with forceRefresh parameter', () async {
  // Setup mock subscription manager
  when(
    mockSubscriptionManager.createSubscription(...),
  ).thenAnswer((_) async => 'sub_123');

  // Mock addRelay to return false (indexer relays not available)
  // This prevents MissingStubError and simulates unavailable indexers
  when(mockNostrService.addRelay(any)).thenAnswer((_) async => false);

  // ... test setup ...

  await service.fetchProfile(pubkey, forceRefresh: true);

  // Expect 2 calls: initial attempt + retry after indexer fallback
  verify(
    mockSubscriptionManager.createSubscription(...),
  ).called(2);
});

Notes

Related Patterns

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.