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
Typemock provides a class called Check.

How is this used? For example, if I use the "IsTypeOf(...)" method, what parameter would this be mapped to checking? I may want to check the 2nd parameter of a method, etc.


Thanks
asked by GSS (2.8k points)

1 Answer

0 votes
Checkers are part of the older API (Reflective/Natural) and can be used from that API:
[Test] 
public void Authenticated ()
{
      Authentication authenticator = new Authentication();
      MockManager.Init ();
      // the Logger class is now being mocked
      Mock logMock = MockManager.Mock(typeof(Logger)); 
      // set up our expectations 
      logMock.ExpectCall("Log").Args(Logger.NORMAL,Check.IsAny());
      logMock.ExpectCall("Log").Args(Logger.FAILED,Check.IsAny());
      
      Assert.IsFalse(authenticator.IsAuthenticated("user","password"));        
      MockManager.Verify();
}


<Test()> Public Sub Authenticated ()
      Dim authenticator As Authentication = new Authentication
      MockManager.Init()
      ' the Logger class is now being mocked
      Dim logMock As Mock = MockManager.Mock(GetType(Logger)) 
      ' set up our expectations 
      logMock.ExpectCall("Log").Args(Logger.NORMAL,Check.IsAny())
      logMock.ExpectCall("Log").Args(Logger.FAILED,Check.IsAny())
      
      Assert.IsFalse(authenticator.IsAuthenticated("user","password"))        
      MockManager.Verify()
End Sub


To find more a about the available checkers see: https://www.typemock.com/Docs/UserGuide/ ... ildIn.html

In the newer AAA API we use DoInstead that uses a delegate to check for specific arguments:
[TestMethod]
[Isolated]
public void TestDoInstead()
{
    var fake = Isolate.Fake.Instance<ProductShelf>();

    // When GetProductQuantity will be called, our anonymous delegate will run.
    // GetProductQuantity will return the value based on the custom logic inside the delegate.
    Isolate.WhenCalled(()=> fake.GetProductQuantity("")).DoInstead(callContext=>
       {
           // Access method arguments
           string name = callContext.Parameters[0] as string;
           if(name == "MyProduct")
           {
               return 10;
           }

           return 5;
       });

    // Assert - out custom behavior is returned when the method is called
    Assert.AreEqual(10, (int)fake.GetProductQuantity("MyProduct"));
    Assert.AreEqual(5, (int)fake.GetProductQuantity("OtherProduct"));
}
answered by dhelper (11.9k points)
...