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

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

2 Answers 99 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Lars
Top achievements
Rank 1
Lars asked on 08 Aug 2019, 09:11 AM

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 Answers, 1 is accepted

Sort by
0
Accepted
Lars
Top achievements
Rank 1
answered on 08 Aug 2019, 10:10 AM

I found the solution myself.

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

0
Ivo
Telerik team
answered on 08 Aug 2019, 10:30 AM
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
Tags
General Discussions
Asked by
Lars
Top achievements
Rank 1
Answers by
Lars
Top achievements
Rank 1
Ivo
Telerik team
Share this question
or