This is a migrated thread and some comments may be shown as answers.

Automocked Async Methods Never Complete

1 Answer 114 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Micah
Top achievements
Rank 1
Micah asked on 19 Jan 2015, 05:50 AM
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.

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 19 Jan 2015, 08:45 AM
Hi Micah,

Thank you for your excellent suggestion. This feature is already on our to-do list and it will be made available in one of the internal builds following our upcoming official release.

Regards,
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
General Discussions
Asked by
Micah
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or