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 want to clear all isolations on non public methods, properties

how can this be achieved?
asked by tolisss (28.8k points)

8 Answers

0 votes
Hi Tolisss,

There is no API that explicitly does what you are describing. In principal, if you are running your tests inside the [Isolated] tag all behaviors are cleared when the test is over. The same is true for using Isolate.CleanUp(). However, these clear up public as well as non-public fake behaviors.

Can you describe the scenario you would like to clear up only non-public behaviors on?

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Hi Doron

here is the scenario i am using .
all my classes inherit a compex Base class
first i Isolate the base so a recursive fake will be return for all public properties methods.

then i write the test method on my derived class
then i write the implementation of the method
then the method passes and i do refactor it
as u understand my test fail after refactor cause i i rafactor my public mehtos to smaller private ones.
so i need a way to clear all nonpublic isolation to my derived classs

or if my test approach doesn't look ok please suggest a better one

ps:my email subscription problem has never beeen fixed
answered by tolisss (28.8k points)
0 votes
Tolisss,

Did you refactor the private methods and pushed them up to the base class? Otherwise they should not be faked if the instance of the derived class is not faked.

Can you post here a code snippet demonstrating this issue?

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Hi Doron

sorry there was some mistypes on my previous post so u did not understand it correclty here it is again

first i Isolate myclass so a recursive fake will be return for all public properties/methods of base class.

then i remove from Isolation of the method that i am testing
then i write the test for that method
then i write the implementation of the method
then the test passes and i refactor the method that i am testing

as u understand my test fail after refactor cause the class is Isolated

it is very time consuming to go back to my test and clear all Isolation for each private method manually
so i need a way to clear all nonpublic isolations of my derived classs


or if my test approach doesn't look ok please suggest a better one
answered by tolisss (28.8k points)
0 votes
no answer? :(
answered by tolisss (28.8k points)
0 votes
Hi Tolisss,

Let me see if I understand correctly:
1. You have a complex base class (MyBase)
2. You fake all public calls from a derived class to base - do you use NonPublic for this?
3. You implement the logic inside the derived class and refactor it.

Please correct me if I made any mistake. I do have a few questions:
- Do you change anything inside the base class?
- What do you use NonPublic API for?

Does the logic inside the base class change at any time?

A code example can really help - can you post a simple code snippet that shows what you're trying to accomplish?
answered by dhelper (11.9k points)
0 votes
Hi dhelper

sorry for my late response but without email subscription to my post i cannot follow them easily. Do not know why u can not fix such an easy issue

anyway here is the code
[TestFixture]
    public class TestFixture1
    {
        [Test]
        public void Test()
        {
            var myClass = Isolate.Fake.Instance<MyClass>();
            Isolate.WhenCalled(() => myClass.DoSomething()).CallOriginal();

            int something = myClass.DoSomething();

            Assert.AreEqual(1, something);
        }
    }

    internal class CombpexBaseClass
    {
        public int CompexBaseMethod()
        {
            return 3;
        }
    }

    internal class MyClass:CombpexBaseClass
    {
        public int DoSomething()
        {
            int method = CompexBaseMethod();
            return method+1;
        }
    }

this code passes the test and then i refactor MyClass to something like
internal class MyClass:CombpexBaseClass
    {
        public int DoSomething()
        {
            return doSOmething();
        }

        private int doSOmething()
        {
            int method = CompexBaseMethod();
            return method+1;
        }
    }

of course the test fails and i need a way to unface all private methods of MyClass.


i could something like
foreach (var methodInfo in typeof(MyClass).GetMethods(BindingFlags.Instance|BindingFlags.NonPublic))
            {
                Isolate.NonPublic.WhenCalled(myClass,methodInfo.Name).CallOriginal();
            }


but i get an exception

Cannot mock methods defined in types from mscorlib assembly.
answered by tolisss (28.8k points)
0 votes
Hi Tolisss

Currently the solution to your problem is to explicitly set the behavior of the private methods.

You can use the following helper method that will set all private methods of faked instance to call original but without throwing the "mscorlib exception":
public void SetCallOriginalOnPrivateMethods(object isolatedInstance)
{
    Type type = isolatedInstance.GetType();
    foreach (var methodInfo in type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic))
    {
        if(! methodInfo.DeclaringType.Assembly.FullName.StartsWith("mscorlib"))
        {
            Isolate.NonPublic.WhenCalled(isolatedInstance, methodInfo.Name).CallOriginal();
        }
    }
}


and use it like this:
[Test]
public void Test()
{
    var myClass = Isolate.Fake.Instance<MyClass>();
    Isolate.WhenCalled(() => myClass.DoSomething()).CallOriginal();
    SetCallOriginalOnPrivateMethods(myClass);

    int something = myClass.DoSomething();
    Assert.AreEqual(1, something);
}


Please let me know if it helps.
answered by ohad (35.4k points)
...