@@ -23,8 +23,7 @@ RequestContext createMockRequestContext({
23
23
// Stub the request getter
24
24
when (() => context.request).thenReturn (effectiveRequest);
25
25
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.
28
27
dependencies.forEach ((type, instance) {
29
28
// Add specific stubs for known types. Extend this list as needed.
30
29
if (type == AuthService ) {
@@ -34,47 +33,30 @@ RequestContext createMockRequestContext({
34
33
when (() => context.read <HtDataRepository <User >>())
35
34
.thenReturn (instance as HtDataRepository <User >);
36
35
} else if (type == RequestId ) {
37
- // Handle RequestId specifically if provided
38
36
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 ? );
39
41
}
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.
46
44
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
49
46
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 .' ,
52
49
);
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
- }
63
50
}
64
51
});
65
52
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.
75
57
76
58
// 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
78
60
// to allow chaining, which is typical for provider middleware.
79
61
// Corrected: provide takes one argument (the provider function).
80
62
// Use `any<T>()` with explicit type argument for the function.
0 commit comments