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
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 by vikaschoithani (2.8k points)

3 Answers

0 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 by dhelper (11.9k points)
0 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 by vikaschoithani (2.8k points)
0 votes
Okay,

Got it..! :)
answered by vikaschoithani (2.8k points)
...