using
System;
using
System.Threading.Tasks;
using
Telerik.JustMock;
using
Xunit;
namespace
Test
{
public
class
ClassTests
{
public
interface
IAsync
{
Task<String> MyTask();
}
[Fact]
public
async Task fails_due_to_timeout()
{
var mocked = Mock.Create<IAsync>();
var result = await mocked.MyTask();
}
[Fact]
public
async Task passes()
{
var mocked = Mock.Create<IAsync>();
Mock.Arrange(() => mocked.MyTask()).Returns(Task.FromResult<String>(
null
));
var result = await mocked.MyTask();
}
}
}
I believe that the more useful auto-mocking behavior here would be for the returned task to be in a completed state containing an auto-mocked T. For example, if the function to be mocked returns a Task<IMyInterface>, I believe the automocker should return the equivalent of Task.FromResult(Mock.Create<IMyInterface>()).
When writing async friendly code, the auto-mocker isn't very useful because none of my async methods never complete. I have to manually Mock.Arrange every method so it will return a mocked Task.FromResult. In most cases, I would prefer the happy path be default (tasks complete with results) rather than the unhappy path be the default.