Hi guys I have a method like this:
public virtual async Task StartSearch()
{
CancellationTokenSource = new CancellationTokenSource();
Context = ContextFactory.Create();
SearchResults = await Task.Factory.StartNew(() =>
{
try
{
IsLoading = true;
var resultTask = GetResults().ToListAsync(CancellationTokenSource.Token);
while (!resultTask.IsCompleted && !resultTask.IsFaulted &&
!CancellationTokenSource.Token.IsCancellationRequested)
Thread.Sleep(100);
IsLoading = false;
if (resultTask.IsFaulted)
throw resultTask.Exception ??
new Exception("Some error.");
return resultTask.IsCompleted && !resultTask.IsCanceled ? resultTask.Result : null;
}
catch (Exception)
{
IsLoading = false;
throw;
}
}, CancellationTokenSource.Token) ?? SearchResults;
}
I wonder how could I mock up a Task (with all those IsFaulted, IsCompleted, IsCaneled fields) which I get from .ToListAsync() method? I've found that they are not virtual so I cannot create a Task object to simply mock them. Additional question is if it gives me something if I create my own dummy class and mock it for a test.
What are your suggestions? Maybe you don't want to mock it?
For now I only tested this to check if my method will return some data.
EDIT:
public abstract IQueryable<dynamic> GetResults();
and some exemplary implementation:
public override IQueryable<dynamic> GetResults()
{
var query = Context.Table.AsQueryable();
query = string.IsNullOrWhiteSpace(Field)
? query
: query.Where(table => table.Field.Contains(SomeFilter));
return query.Select(
table => new
{
table.Field1,
table.Field2,
table.Field3
}).OrderBy(table => table.Id);
}