-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add support for middleware testing on dart_frog_test
#1101
Comments
renancaraujo
added
feature
A new feature or request
and removed
bug
Something isn't working as expected
labels
Oct 10, 2023
renancaraujo
changed the title
fix:
feat: Add support for middleware testing on Oct 10, 2023
dart_frog_test
has trouble testing middlewaredart_frog_test
Hello @mtwichel , Thanks for opening an issue! Middleware testing was not in scope for the first version of |
Sounds good @renancaraujo! For your consideration, since opening this issue I've been trying to solve it for my project and settled on this implementation of import 'package:dart_frog/dart_frog.dart';
class TestRequestContext implements RequestContext {
TestRequestContext({
this.path = '/',
this.basePath = 'https://example.com',
this.method = HttpMethod.get,
this.headers,
this.body,
});
String path;
String basePath;
HttpMethod method;
Map<String, String>? headers;
Object? body;
Map<String, dynamic> _dependencies = {};
@override
RequestContext provide<T extends Object?>(T Function() create) {
final dependency = create();
_dependencies[dependency.runtimeType.toString()] = dependency;
return this;
}
void mockProvide<T>(T dependency) => _dependencies[T.toString()] = dependency;
@override
T read<T>() {
final dependency = _dependencies[T.toString()];
if (dependency is! T) {
throw Error();
}
return dependency;
}
@override
Request get request => Request(
method.name.toUpperCase(),
Uri.parse('$basePath$path'),
headers: headers,
body: body,
);
} It has a few advantages imo
test('injects a RequestLogger', () async {
final context = TestRequestContext();
RequestLogger? logger;
Future<Response> handler(RequestContext context) async {
logger = context.read<RequestLogger>();
return Response();
}
final newHandler = middleware.middleware(handler);
await newHandler(context);
expect(logger, isNotNull);
});
Just my experience working with it so far. 💙 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
A clear and concise description of what the bug is.
Steps To Reproduce
_middleware.dart
with_middleware_test.dart
with these contents3.Run the test and you'll get
Expected Behavior
I would expect the test to pass.
Additional Context
I believe this is an issue because the underlying
RequestContext
returned fromcontext.context
is a_MockRequestContext
that has no stub implementation for.provide
, so when theprovider
middleware is called, it produces that error.dart_frog/packages/dart_frog_test/lib/src/test_request_context.dart
Lines 41 to 52 in dc62b2b
Another way to test middleware proposed by @felangel is here: https://github.com/VeryGoodOpenSource/dart_frog/blob/main/examples/kitchen_sink/test/routes/_middleware_test.dart
This is a nice solution because it doesn't require
.read
to be stubbed, but I'm not sure how it could be generalized in a package like this.Also happy to be shown how I'm doing it wrong because I hope I am 😆
Thanks 💙
The text was updated successfully, but these errors were encountered: