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
I have a constructor with a single private method.  How can I fake the constructor and use Isolate to dictate to ignore the private method that\'s called so that I can verify it was called in my Assert part of the test.

public class MyClass

{

     public MyClass()

     {

          Foo();

     }

     private void Foo()

     { // does stuff }

}
asked by Batman117 (600 points)

2 Answers

0 votes

Hi,

From what I understand, you are trying to invoke the constructor yet ignoring the private method that's called in it.

In this case, there is no reason to fake the constructor, as soon as you create an instance of the object the constructor will be called.

all that's left to do is to make sure that the private method will be ignored and finally verify that the "Foo" method wasn't called.

For example, you can see the following test:

 [TestMethod]

        public void ClassWithConstructor()   {

            var real = new MyClass();

            Isolate.NonPublic.WhenCalled(real,"Foo").IgnoreCall();

            Isolate.Verify.NonPublic.WasNotCalled(real, "Foo");

        }

If that was not your attention please describe it in more details and we will be glad to help.

answered by Alon_TypeMock (8.9k points)
I want to verify that Foo WAS called, but I don\\\'t want it\\\'s logic to execute in my constructor test.  If I instantiate MyClass, then Foo will be called.  My understanding is the IgnoreCall() just prevent\\\'s it\\\'s logic from firing.

So in your example above, I want my Verify statement to be Isolate.Verify.NonPublic.WasCalled(real, \\\"Foo\\\");.  I get an error with this code saying that Foo was expected but not called.

I know this would work if I was verifying Foo() from another method inside MyClass().  It\\\'s the fact that Foo() is being called from the constructor that is making it complicated for me.  Is there a way TypeMock to intercept method calls within a constructor up object instantiation?
Hi,

I couldn\'t understand your question.

On one hand you want to ignore Foo( ) when it\'s called, and on the other hand, you want to verify that Foo() was called.

Can you please elaborate again about your question?

P.S: the ignore API prevents the method from being called completely, not just prevents the logic from firing.
Take a look at this test:

[TestMethod]

     public void ClassWithConstructor2()  {

          var real = new MyClass();

           Isolate.NonPublic.WhenCalled(real,\"Foo\").IgnoreCall();

           Isolate.Verify.NonPublic.WasNotCalled(real, \"Foo\");

       }

In this test we fake the next instance of MyClass and make sure that when Foo() is called it will be ignored.

in this part of the test, there is no initialization of MyClass.

next, we create an instance of MyClass and verify that Foo() was not called.

Try to debug this test and see that in the constructor, when Foo() is supposed to be called, it is actually not called and you go straight to the next line.
0 votes

Hi,

Did my answer help you?

Please keep us posted.

Cheers,

Alon Sapozhnikov

answered by Alon_TypeMock (8.9k points)
...