a have this code:
var response = await await _service.GetProfile(code)
.ContinueWith(async task =>{
if(task.IsFaulted)
{
///... anyway
return object;
}
});
I can't get to IsFault in my unit test. Can you help me build this unit test? I'm using Xunit and NSubstitute
Don't use ContinueWith
. Just use await
. A faulted Task
will throw an exception on await
that you can catch
and handle.
try
{
var response = await _service.GetProfile(code);
// Here Task is complete without an exception
}
catch(Exception ex)
{
// Task has faulted
return object;
}
Note that your test method has to be marked async
.
In order to write unit tests for asynchronous code without using await, use synchronization objects like a ManualResetEvent
.
See my answer here: "Thread was being aborted." in test that tests code that fires a delegate on a background thread
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.