behavior on simple fakes applies to ALL instances

4 posts, 0 answers
  1. lior friedman
    lior friedman avatar
    2 posts
    Member since:
    Apr 2010

    Posted 06 Nov 2011 Link to this post

    The following test fails:
    namespace JustMockExamples
    {
        class UsedClass
        {
            public int ReturnFive()
            { return 5; }
        }
     
        [TestClass]
        public class InstancesBug
        {
            [TestMethod]
            public void SimpleFake_ControlAllInstances()
            {
                var fakeUsed = Mock.Create<UsedClass>();
                Mock.Arrange(() => fakeUsed.ReturnFive()).Returns(7);
     
                Assert.AreEqual(7, fakeUsed.ReturnFive());
     
                var realInstance = new UsedClass();
                Assert.AreEqual(5, realInstance.ReturnFive());
            }
        }
    }

    Any idea why?
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 09 Nov 2011 Link to this post

    Hi Lior,

    Thanks again for reporting the issue. I found this bug with the latest release. This is happening due to future mocking settings. However if you move the real instance code to a different test method, it will work as expected.
    [TestMethod]
    public void SimpleFake_ControlAllInstances()
    {
        var fakeUsed = Mock.Create<UsedClass>();
        Mock.Arrange(() => fakeUsed.ReturnFive()).Returns(7);
        Assert.AreEqual(7, fakeUsed.ReturnFive());
    }
     
    [TestMethod]
    public void SimpleFake_ReadInstance()
    {
        var realInstance = new UsedClass();
        Assert.AreEqual(5, realInstance.ReturnFive());
    }

    Here i named the test in a way that mocked instance will be called first and then the real one. However if real instance executes first and invokes the OnJITCompilation started, you can still resolve the situation using the following block:

    static InstancesBug()
    {
    // puts a dummy interceptor
    //so that calling real instance first wont break the test.
    Mock.Partial<UsedClass>().For(x => x.ReturnFive());
    }


    Moreover, the bug in your test is valid as well and the fix will be available by end of this week. You can create a support ticket where i will send an internal build. However, it will also be available in the Q3 build coming next week.
     

    Kind Regards,
    Mehfuz
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft R3 2016 release webinar banner
  4. lior friedman
    lior friedman avatar
    2 posts
    Member since:
    Apr 2010

    Posted 09 Nov 2011 Link to this post

    Hi Mehfuz,

    Actually, I found this defect since i had a test which involved two instances.
    one which i need to fake as a "future mock" and one which i needed to fake as a regular mock.
    i found out that no matter what i did all behaviors (for all instances) were taken from the future mock only.

    I'm guessing that as long as i have a single instance of the same type I'm safe.

    In any case ill wait for the Q3 build and check it out again

    thank you
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 09 Nov 2011 Link to this post

    Hi Lior,

    Thanks again for the prompt reply. Once the fix is ready i will write you back again. In the meantime, let me know if there is something else that you might find or i can help with.

    Kind Regards,
    Mehfuz
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top