I am having some trouble when unit testing using expect(method, throwsA)
So I have a Future function that is doing some tasks and is catching a SocketEXception when it pops, then throws an Exception with a better message.
Here's a sample code that you can use that triggers my problem when testing it:
Future<void> testFunction() async{
try{
throw SocketException("bad error message");
} on SocketException catch (_) {
throw Exception("My custom message");
}
}
test('test function', () async {
expect(
testFunction(),
throwsA(Exception(
"My custom message")));
});
and here's the output of the test:
Expected: throws _Exception:<Exception: My custom message>
Actual: <Instance of 'Future<void>'>
Which: threw _Exception:<Exception: My custom message>
I don't know why the test isn't working as it expects a throw, and the actual is throwing the exact same error, I might be doing something wrong as I'm a beginner, if someone can help me understand why it isn't working, it would be cool.
Thanks.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…