Repository navigation
Testing with Future.error #23
Copy link
Copy link
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
I am trying to test my async function which accepts a Future and also returns a Future.
If the input Future throws an error, I handle it and return a -1 instead.
e.g.
Main Code
class Processor {
Future<int> execute(Future<int> process) async {
try {
final result = await process;
return result;
} catch (e) {
return -1;
}
}
}Test
void main() {
final processor = Processor();
final process = Future.value(1);
getErroneousProcess() => Future<int>.error(SignalException("something went wrong"));
test('exception', () async {
final result = await processor.execute(getErroneousProcess());
expect(result, -1);
});
test('success', () async {
final result = await processor.execute(process);
expect(result, 1);
});
}Parameterized version
void main() {
final processor = Processor();
final process = Future.value(1);
getErroneousProcess() => Future<int>.error(SignalException("something went wrong"));
parameterizedTest(
'process',
[
[getErroneousProcess(), -1],
[process, 1],
],
(process, expected) async {
final result = await processor.execute(process);
expect(result, expected);
},
);
}Problem
Expectation
The parameterized version runs and tests code the same way as non-parameterized version.
Reality
The parameterized test fails with output did not complete [E]
$ flutter test
00:00 +0 -1: loading my_project/test/test_error.dart [E]
Failed to load "my_project/test/test_error.dart": something went wrong
00:00 +0 -1: process [ Instance of 'Future<int>', -1 ] - did not complete [E]
00:00 +0 -1: process [ Instance of 'Future<int>', 1 ] - did not complete [E]
00:00 +0 -1: Some tests failed.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working
Activity
Guldem commentedon Jun 3, 2025
Thanks for reporting this , including a example. I have to look into this. I would suspected it to work the same.
Guldem commentedon Jun 27, 2025
I had some time to look into it and it looks like it has to do with how the
Future.erroris passed around. I don't really think its a bug.For example this results in the same issue:
The problem is how the error is provided. For the parameterized version the callback isn't stored in the list but the value which causes the test to fail.
To solve this in the parameterized version is to provide the correct callback like this:
I hope this helps with your issue 😄
shripal17 commentedon Jun 29, 2025
Thanks for your time, that explains. So basically, the solution is to use supplier functions for
Future.errors.Just an idea for you to consider: Maybe this is also worth documenting? 📚