Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

How to test a call to a method should not be made

+2 votes
Code:
public class MyClass
{
        public bool MethodX(long parameterX)
        {
            bool retVal = false;
            ...code...
            if (this.PropertyX != null && this.PropertyX == parameterX)
            {
                PrivateMethodY();
            }
            ...code...
            return retVal;
        }

        private void PrivateMethodY()
        {
            ...code...
        }

        public long PropertyX
        {
              get
              {
                   ...Code...
              }
              set
             {
                   ...Code...
             }
        }
}


In the given scenario above, I want to test if PropertyX != parameterX, call to PrivateMethodY should not be made.

Test:
        [TestMethod, VerifyMocks]
        public void MethodXTest()
        {
            MyClass_Accessor target = new MyClass_Accessor();
            using (RecordExpectations r = RecorderManager.StartRecording())
            {
                r.ExpectAndReturn(target.PropertyX, (long)11);
                r.FailWhenCalled(target.PrivateMethodY());
            }
            Assert.IsTrue(target.MethodX(10));
        }



Since PrivateMethodY() retruns void, FailWhenCalled can not be used as it takes 'object' parameter.
I want to know if there is a way to test this and test will fail if a call is made to 'PrivateMethodY()'

Thank you,
Vikas
asked Mar 2, 2009 by vikaschoithani (780 points)

3 Answers

+2 votes
Is there a reason you're not using AAA (newer) API?

[TestMethod]
public void MethodXTest()
{
     MyClass_Accessor target = new MyClass_Accessor(); 
     Isolate.WhenCalled(() => target.PropertyX).WillReturn(11);

     Assert.IsTrue(target.MethodX(10));
     Isolate.Verify.NonPublic.WasNotCalled(target,  "PrivateMethodY");
}


Otherwise you can use FaileWhenCalled without any arguments:
using (RecordExpectations r = RecorderManager.StartRecording())
{
      r.ExpectAndReturn(target.PropertyX, (long)11);

      // fail if PrivateMethodY was called
      target.PrivateMethodY();
      r.FailWhenCalled();
} 


And don't forget to use MockManager.Verify at the end of the test.
answered Mar 3, 2009 by dhelper (23,700 points)
+2 votes
Though till now I didn't feel the need to use AAA, now that you are suggesting it and I read couple of blogs on this site. I am excited to learn it and would like to use it.

I have an Enterprise Edition License and was able to use Natural Mock, but I am not able to use any AAA features. I uninstalled 5.2.1 and installed latest 5.2.2, but still not able to resolve the issue.

The message I am getting is, "The name 'Isolate' does not exist in current context."

Do I have to add any references? Can you help?

Thanks,
Vikas
answered Mar 5, 2009 by vikaschoithani (780 points)
+2 votes
Okay,

Got it..! :)
answered Mar 5, 2009 by vikaschoithani (780 points)
...