chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hi,

I am currenlty currenlty evaluating TypeMock.Net using Community Edition for my development projects, and if it works well will going to get a Pro license. However, I encountered some problem on instance mocking. Coould someone please help to figure it out?

When I tried to mock a property getter of a .net framework class, it fails. However, I can successfully mock a similar getter of my own class, please refer to the code below,

/// <summary>
        ///A test for mocking property getter from .Net Framework class
        ///</summary>
        [TestMethod()]
        public void BoolPropertyGetterFromNetFxTest()
        {
            MockManager.Init();

            // ActiveDirectoryAccessRule is the class to be mocked.
            MockObject mock = MockManager.MockObject(typeof(ActiveDirectoryAccessRule),
                new SecurityIdentifier("S-1-5-32-545"),
                ActiveDirectoryRights.ReadProperty,
                AccessControlType.Allow,
                Guid.Empty, ActiveDirectorySecurityInheritance.All);
            mock.ExpectGetAlways("IsInherited", true);

            ActiveDirectoryAccessRule rule = (ActiveDirectoryAccessRule)mock.Object;
            Assert.IsTrue(rule.IsInherited, "Failed to mock IsInherited");

            MockManager.Verify();
        }

        /// <summary>
        /// A test for mocking property getter from my own code
        /// </summary>
        [TestMethod()]
        public void BoolPropertyGetterFromOwnCodeTest()
        {
            MockManager.Init();

            MockObject mock = MockManager.MockObject(typeof(MyClass));
            mock.ExpectGetAlways("BoolProperty", true);

            MyClass myObj = (MyClass)mock.Object;
            Assert.IsTrue(myObj.BoolProperty, "Failed to mock my own proeprty");

            MockManager.Verify();
        }


And the MyClass definition is as follow,

    /// <summary>
    /// Class of property getter
    /// </summary>
    public class MyClass
    {
        private bool _myBool;

        public MyClass()
        {
            _myBool = false;
        }

        public bool BoolProperty
        {
            get { return _myBool; }
        }
    }

________
Ford iosis history
asked by Tony.Cheung (1.8k points)

4 Answers

0 votes
Hi Tony,
You have found a bug :twisted:
The reason for this is that IsInherited is actually a method in the AuthorizationRule class which is in the mscorlib.dll.
We don't mock this dll, as this leads to really strange behaviors of the system. We are working on ways to mock these too.

The bug is that we didn't write an error while recording the expectation.

A workaround would be to either setup the system so that the value is what you expected or to check that value using your own method and mocking that method
public static bool IsInherited(ActiveDirectoryAccessRule rule)
{
   return rule.IsInherited;
}
answered by scott (32k points)
0 votes
Thanks for your help, Scott!

So I have to use this workaround for every classes from mscorlib.dll when mocking objects? How about mocking methods of classes in mscorlib.dll, should I also do like

public static bool CheckSomething(ClassFrom_mscorlib arg)
{
    return arg.CheckSomething();
}


If that is the case, then to mock a method with different arguments like,
public static bool CheckSomethingDifficult(ClassFrom_mscorlib arg, int para1, double para2, string para3, bool para4)
{
    return arg.CheckSomethingDifficult(para1, para2, para3, para4);
}


With this workaround, whenever we mock objects from mscorlib, we have to provide a static method just for unit testing. This may,
1. introduce lot of APIs change to enable unit testing, and
2. all callers should call our APIs in order to not break the tests

Any alternatives?
________
Honda Wiki
answered by Tony.Cheung (1.8k points)
0 votes
Yup,
We know this, and this is one of our high priority features.
answered by scott (32k points)
0 votes
Thanks for your prompt replies, Scott. I will wait for this :)
________
Vapor Lounge
answered by Tony.Cheung (1.8k points)
...