Mocking Timers using Future Mocking

2 Answers 129 Views
General Discussions
Phillip
Top achievements
Rank 1
Iron
Phillip asked on 11 May 2023, 08:08 PM

Hello,

I have the following code, as a test, but it is not working at all.

var timer1 = new Timer(500);
Mock.Arrange(() => new Timer(Arg.IsAny<double>())).Returns(timer1);
var sameTimer = new Timer(200);
Assert.AreEqual(timer1,sameTimer);

 

The use case, is that we have lots of timers in our legacy code that need mocking.  They are not injected so I would like to override the constructor if possible and return a different object.   Is this doable?

Thanks 

Phillip Davis

2 Answers, 1 is accepted

Sort by
0
Ivo
Telerik team
answered on 15 May 2023, 03:17 PM

Hello Phillip,

Thank you for letting us know about this interesting case. In fact, the scenario Is doable with JustMock, but the test does not reflect it fully. I have tested the code and for some reason, JustMock intercepts the whole Timer class hierarchy and that is why it returns a different instance instead of arranged one, strange but true. After that, I decided to pull out the calls to the arranged constructors out of the test and it started to work as expected, here is the code:

public class UnitTest1
{
    public static Timer GetNew()
    {
        return new Timer(200);
    }
    
    [TestMethod]
    public void TestMethod1()
    {
        var timer1 = new Timer(500);
        Mock.Arrange(() => new Timer(Arg.IsAny<double>())).Returns(timer1);

        var sameTimer = GetNew();

        Assert.AreEqual(timer1, sameTimer);
    }
}

It is hard for me to state for the limited time that I have had for investigation whether it is a bug or not, but I would say for sure that the modified code fulfills the described purpose and brings the desired behavior. I hope it fits your needs.

Regards,
Ivo
Progress Telerik

A brand new JustMock course was just added to the Virtual Classroom - the free self-paced technical training portal that gets you up to speed with Telerik and Kendo UI products! Check it out at https://learn.telerik.com.
0
Phillip
Top achievements
Rank 1
Iron
answered on 05 Jun 2023, 07:03 PM
Hi Ivo!  

So I know in my example, the timer was being constructed alongside the mock.

But in our legacy code base reality, we have a lot of timers that are just create without being passed into the constructor. 

So it would be super cool if I could just tell telerik, when you see this class created with this parameter, anywhere during this test execution, return this mocked one instead.  

That would be super handy. 
The example you gave me above, if I understand it correctly, is dependent on the timer being injected in through the constructor, which is not always the case in our legacy code.  
Ivo
Telerik team
commented on 08 Jun 2023, 08:34 AM

As an alternative to mocking the object instantiation, it could be possible to fake the methods and properties of all instances of a given type, here you can find details on how exactly. 
Tags
General Discussions
Asked by
Phillip
Top achievements
Rank 1
Iron
Answers by
Ivo
Telerik team
Phillip
Top achievements
Rank 1
Iron
Share this question
or