Don't use the Task
constructor in async
code. If you just need a completed Task
with a return value, use Task.FromResult
:
IUser user = new User();
Fakes.ShimUserManager.AllInstances.LoginAsyncString = (um, u) => Task.FromResult(user);
As an additional tip, it's a good idea to cover these cases in your unit tests:
- Synchronous success (
Task.FromResult(user)
). - Asynchronous success (
Task.Run(() => user)
). - Asynchronous error (
Task.Run(() => { throw new InvalidOperationException("or whatever"); return user; })
).