http-mock-adapter
http-mock-adapter copied to clipboard
Is there a way to have MockServerCallback's server parameter be strongly typed?
I will write test code like this:
dioAdapter.onPost("some_route", (server) => server.reply(...), data: Matchers.any);
I am very much bugged that I do not know the type of 'server'. Moreover, I don't really know what to pass to server.reply()!
What I've tried: I've looked through the relevant docs, but I see no page on MockServerCallback. I've looked into the code and tried declaring the type of the callback that I pass into onPost(), but my linter won't allow me.
Its type is basically dynamic. And basing on exact type is not allowed yet. Would you like to have kinda feature?
I think the problem is not that the callback is not strongly typed because it is (it is defined as a typedef of type void Function(MockServer server);
), but rather than MockServerCallback
and MockServer
types are not exposed by the package.
In my case I would like to reuse the MockServerCallback function between multiple adapter methods but I can't because of types not being exposed.
Example of what I would like to do:
adapter.onGet('/some/route', replyCallback);
adapter.onPost('/some/route', replyCallback);
adapter.onPut('/some/route', replyCallback);
void replyCallback(MockServer server) {
server.reply(200, {'message': 'success'});
}
Right now the compiler complains about MockServer
that is undefined because it cannot see it.
Could we expose it ? I can propose a PR
I think the problem is not that the callback is not strongly typed because it is (it is defined as a typedef of type
void Function(MockServer server);
), but rather thanMockServerCallback
andMockServer
types are not exposed by the package.In my case I would like to reuse the MockServerCallback function between multiple adapter methods but I can't because of types not being exposed.
Example of what I would like to do:
adapter.onGet('/some/route', replyCallback); adapter.onPost('/some/route', replyCallback); adapter.onPut('/some/route', replyCallback); void replyCallback(MockServer server) { server.reply(200, {'message': 'success'}); }
Right now the compiler complains about
MockServer
that is undefined because it cannot see it.Could we expose it ? I can propose a PR
@theiskaa thoughts?