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

behavior on simple fakes applies to ALL instances

3 Answers 84 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
lior friedman
Top achievements
Rank 1
lior friedman asked on 06 Nov 2011, 02:36 PM
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?

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 09 Nov 2011, 09:01 AM
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 >>

0
lior friedman
Top achievements
Rank 1
answered on 09 Nov 2011, 09:10 AM
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
0
Ricky
Telerik team
answered on 09 Nov 2011, 11:28 AM
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 >>

Tags
General Discussions
Asked by
lior friedman
Top achievements
Rank 1
Answers by
Ricky
Telerik team
lior friedman
Top achievements
Rank 1
Share this question
or