Skip to content

Commit 5a12108

Browse files
committed
refactor(test): improve mock request context
- Remove generic read stubbing - Add explicit User stub - Improve warning message
1 parent 887b93f commit 5a12108

File tree

1 file changed

+15
-33
lines changed

1 file changed

+15
-33
lines changed

test/helpers/create_mock_request_context.dart

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ RequestContext createMockRequestContext({
2323
// Stub the request getter
2424
when(() => context.request).thenReturn(effectiveRequest);
2525

26-
// Stub the read<T>() method for each provided dependency.
27-
// Use specific types for clarity and type safety.
26+
// Stub the read<T>() method for each explicitly provided dependency.
2827
dependencies.forEach((type, instance) {
2928
// Add specific stubs for known types. Extend this list as needed.
3029
if (type == AuthService) {
@@ -34,47 +33,30 @@ RequestContext createMockRequestContext({
3433
when(() => context.read<HtDataRepository<User>>())
3534
.thenReturn(instance as HtDataRepository<User>);
3635
} else if (type == RequestId) {
37-
// Handle RequestId specifically if provided
3836
when(() => context.read<RequestId>()).thenReturn(instance as RequestId);
37+
} else if (type == User) {
38+
// Explicitly handle providing the User object for auth tests
39+
// Note: The type provided should be User, but we stub read<User?>
40+
when(() => context.read<User?>()).thenReturn(instance as User?);
3941
}
40-
// Add other common types here...
41-
// Example for another repository type:
42-
// else if (type == HtDataRepository<Headline>) {
43-
// when(() => context.read<HtDataRepository<Headline>>())
44-
// .thenReturn(instance as HtDataRepository<Headline>);
45-
// }
42+
// Add other specific types used in your tests here...
43+
// e.g., HtDataRepository<Headline>, AuthTokenService, etc.
4644
else {
47-
// Attempt a generic stub for other types, but warn if it fails.
48-
// Using `any()` in read is generally discouraged, prefer specific types.
45+
// Log a warning for unhandled types, but don't attempt generic stubbing
4946
print(
50-
'Warning: Attempting generic stub for context.read<$type>. '
51-
'Consider adding a specific stub in createMockRequestContext.',
47+
'Warning: Unhandled dependency type in createMockRequestContext: $type. '
48+
'Add a specific `when(() => context.read<$type>())` stub if needed.',
5249
);
53-
// This generic stub might not always work as expected.
54-
// Use a specific type if possible, otherwise fallback carefully.
55-
try {
56-
// Stubbing read<dynamic>() can be tricky. Prefer specific types.
57-
// If absolutely needed, ensure the call signature matches.
58-
// Mocktail's `any` matcher doesn't take arguments for `read`.
59-
when(() => context.read<dynamic>()).thenReturn(instance);
60-
} catch (e) {
61-
print('Failed to setup generic read stub for $type: $e');
62-
}
6350
}
6451
});
6552

66-
// Provide a fallback for read<T>() for types *not* explicitly provided.
67-
// This helps catch errors in test setup.
68-
// Corrected: `any()` doesn't take arguments here.
69-
when(() => context.read<dynamic>()).thenThrow(
70-
Exception(
71-
'Dependency not found in mock context. '
72-
'Ensure all required dependencies are provided in the test setup.',
73-
),
74-
);
53+
// IMPORTANT: Remove generic fallbacks for read<dynamic>().
54+
// Tests should explicitly provide *all* dependencies they intend to read.
55+
// If a test tries to read something not provided, Mocktail will throw a
56+
// MissingStubError, which is more informative than a generic exception.
7557

7658
// Stub provide<T>(). It expects a function that returns the value.
77-
// We match any function using `any()` and return the same context
59+
// Match any function using `any()` and return the same context
7860
// to allow chaining, which is typical for provider middleware.
7961
// Corrected: provide takes one argument (the provider function).
8062
// Use `any<T>()` with explicit type argument for the function.

0 commit comments

Comments
 (0)