Automocked Async Methods Never Complete

2 posts, 0 answers
  1. Micah
    Micah avatar
    4 posts
    Member since:
    Aug 2013

    Posted 18 Jan 2015 Link to this post

    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.
  2. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 19 Jan 2015 Link to this post

    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.

     
  3. DevCraft R3 2016 release webinar banner
Back to Top