DoInstead with Delegate with function returning enum values: Returns wrong enum value

3 posts, 1 answers
  1. Lars
    Lars avatar
    2 posts
    Member since:
    Jul 2015

    Posted 08 Aug 2019 Link to this post

    I have a problem when trying to use a DoInstead of a method that returns an enum value.

    The mocked function is indeed called as expected. However, the return value (an enum) is not returned correctly. Instead it always returns the first value of the enum.

    Using Progress Telerik.JustMock 2019.2.620.1 (with license).

    Here is a way to reproduce it (Console application, VS2017, Link to Telerik.JustMock dll):

     

    namespace TelerikJustMockBug
    {
        using System;
        using Telerik.JustMock;
     
        class Program
        {
            delegate CompletionCode MockedReadWriteDelegate(byte[] buffer, int count, out int retCount);
     
            static void Main(string[] args)
            {
                new Program().Run();
            }
     
            private void Run()
            {
                ClassToMock session = Mock.Create<ClassToMock>();
     
                Mock.Arrange(() => new ClassToMock()).Returns(session);
     
                Mock.Arrange(() => session.Read(Arg.IsAny<byte[]>(), Arg.AnyInt, out Arg.Ref(Arg.AnyInt).Value)).DoInstead(new MockedReadWriteDelegate(this.MockedRead));
     
                ClassToMock c = new ClassToMock();
                CompletionCode result = c.Read(new byte[2], 2, out int retCount);
     
                if (retCount != 5)
                {
                    throw new Exception("retCount was not set correctly");
                }
     
                if (result != CompletionCode.NOT_SUCCESS)
                {
                    // this exception is thrown in my case with 'Wrong result: 0'
                    throw new Exception($"Wrong result: {result.ToString()}");
                }
            }
     
            private CompletionCode MockedRead(byte[] buffer, int count, out int retCount)
            {
                retCount = 5;
                return CompletionCode.NOT_SUCCESS;
            }
     
            public enum CompletionCode
            {
                SUCCESS = 1,
                NOT_SUCCESS = 2
            }
     
            public class ClassToMock
            {
                public CompletionCode Read(byte[] buffer, int count, out int retCount)
                {
                    throw new Exception("Real implementation - should never be called due to mocking");
                }
            }
        }
    }
  2. Answer
    Lars
    Lars avatar
    2 posts
    Member since:
    Jul 2015

    Posted 08 Aug 2019 in reply to Lars Link to this post

    I found the solution myself.

    Instead of "DoInstead" a "Returns" is needed.

  3. Ivo
    Admin
    Ivo avatar
    27 posts

    Posted 08 Aug 2019 Link to this post

    Hello Lars,

    I would like to confirm that this is the right solution. By design JustMock ignores the return values in DoInstead clauses, so Returns is the right choice in such case.

    If you have any other questions, please feel free to write back to us.

    Regards,
    Ivo
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top