I'm setting up a consumer for a WebAPI and writing some unit tests. The 'act' portion of my test is as follows:
var dta = await service.AuthenticateAsync(customerId, userName, password, machineId);
try
{
service.AuthenticateAsync(customerId, userName, password, machineId).Wait();
}
catch (AggregateException ex)
{
exUnauthorized = ex;
}
try
{
httpTest.SimulateTimeout();
await service.AuthenticateAsync(customerId, userName, password, machineId);
}
catch (AggregateException ex)
{
exTimeout = ex;
}
I set up Flurl HttpTest as follows:
httpTest.RespondWithJson(auth)
.RespondWith(status: (int)HttpStatusCode.Unauthorized);
In order to get a first response of success and second of unauthorized. As you can see later in the code I set up timeout to test that (if I set it up initially it seems to time out for all requests).
The first call succeeds. The second call where I use Wait()
in the try block works and catches the aggregate exception. The second call where I use await
does not catch the exception; it fails the unit test with the exception thrown by the method.
What am I missing? Why doesn't the await call work right?