Question
Can you test whether a function is actually running Asynchronously?
I'm working on a ASP.NET Web Api project to learn the framework and the testing for it and I was wondering if you could test that Asynchronous methods are actually running Asynchronously (or are they running Synchronously due to any errors for example).
public class RepositoryBase<Tentity, Tcontext> : IRepositoryBase<Tentity> where Tentity : class where Tcontext : DbContext
{
protected Tcontext _RepositoryContext;
protected DbSet<Tentity> dbSet;
public RepositoryBase(Tcontext context)
{
this._RepositoryContext = context;
dbSet = _RepositoryContext.Set<Tentity>();
}
public async Task Create(Tentity entity)
{
await dbSet.AddAsync(entity);
}
}
Can you Test whether the Create method is actually running asynchronously? Is it logical to test whether a method is running asynchronously? Or is it a trivial concern because the fact that it's returning a Task is proof it is running asynchronously.
During unit testing, in my test function, I only tried await repository.Create(testUser);
and then later Asserted that the user inside the database and the one I inputted are identical in all aspects. But that's just testing the end functionality of the Create function and not whether its successfully running asynchronously or not.
My aim is not to test or question this specific framework itself but to question if any generic async function could be tested for its asynchronous nature. The example code above is just the context under which this question appeared in my head.