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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
There seems to be a lot of mistakes in the cheatsheet and API sections.

For example:

ClassToIsolate
ClassToTest
MyType

And more objects don't exist.

Am I missing something? I have all the references in, or are the sections just wrong?

Thanks
Other than that, great product!
asked by GSS (2.8k points)

13 Answers

0 votes
Hi,

Lets take this offline, I'll send you a mail with details.
It would be much help if you can direct me to the exact pages of the errors that you see.
answered by lior (13.2k points)
0 votes
I am having the same problem. I am trying to use PrivateMethods or ClassToMock and I can't find them anywhere outside of the code examples. Please help!
-T
answered by trp (4.1k points)
0 votes
Hi,

Looking at the how-tos on the site I see they should be cleaned up. Basically there are three APIs Isolator provides: the AAA API, and the legacy Reflective and Natural APIs. I would recommend learning the AAA API as that's the one we develop most features for, and lends itself to more readable, less brittle tests.

If you sent a bit more info on what you're trying to do I would be able to point you at the right documentation pages on our developer's guide: https://www.typemock.com/Docs/UserGuide/index.php.

Specifically for faking private methods your entry point should be Isolate.NonPublic - for instance:
// create fake instance
var fake = Isolate.Fake.Instance<ClassToFake>();

// fake a call to private method DoSomething():
Isolate.NonPublic.WhenCalled(fake, "DoSomething").WillReturn(3);


Please let me know if this helps.

Thanks,
Doron
Typemock Support
answered by doron (16.5k points)
0 votes
Sorry but it doesn't help. No yet, anyway. First, the link appears to be broken. :?: Second, I have been using the MockManager - Which one is that? Third, I haven't been able to find a good resource for mocking private static methods using the Isolate.Syntax; this used to work when using the following code:

MockManager.Mock<StaticClass>().ExpectAndReturn( "StaticMethlod", returnValue ).Args( expectedValue ); 


Since I upgraded to 5.3 (then after reinstalling with the patch from Dror*) it is as if I did not mock anything.

*Before receiving a patch from Dror I was getting an exception (see: https://www.typemock.com/community/viewtopic.php?t=1282 ). Now I don't get an exception when the mocked method is called but the mocked method is called as through I had not mocked anything.

I will post my findings with Isolate to let you know how that goes asap.

Thanks,
Tom
answered by trp (4.1k points)
0 votes
I used the code you submitted (I updated the variable names) and my protected static method still executed (and failed) instead of being mocked.

Any ideas?

10x
-T
answered by trp (4.1k points)
0 votes
PS the call I made was this

where

where GetBaseType : ReadOnlyBase<ReadOnlyBO>
// AND
expected = MockManager.MockObject<ReadOnlyBO>().MockedInstance;
// ARE PRECONDITIONS TO THE CODE BELOW


and where the following code preceded the call to the test method

            // create fake instance 
            var fake = Isolate.Fake.Instance<GetBaseType>();

            Isolate.NonPublic.WhenCalled( fake, "GetObject" ).WillReturn( expected );



In Debugger the method being tested is called. That is expected. The method being tested calls the GetObject method which "StepInto" takes the cursor into the definition for GetObject. This is not expected because the Isolate call above should have caused the method body of GetObject to be passed over and the value in expected should be returned.

10x,
T
answered by trp (4.1k points)
0 votes
Nothing I do works. I have made the static method public, I removed the abstract keyword from the class that contains said method and I still can't get it to work. At least now I am getting an exception:


Test method McKesson.ECSG.UT.BO.Shared.XReadOnlyUnitTest.TestStaticAdmDiaReadOnlyGetAdmDiaReadOnly threw exception:  System.TypeLoadException: Method 'ReturnRecursiveFake' in type 'TypeMock.ArrangeActAssert.Recorder`1' from assembly 'Typemock.ArrangeActAssert, Version=5.3.0.0, Culture=neutral, PublicKeyToken=3dae460033b8d8e2' does not have an implementation..


Step 1: I want to mock a static call.
Step2: I want to mock a protected static call.
Step 3: I want to mock a static call on an abstract class.
Step 4: I want to mock a protected static call on an abstract class.

I can't seem to do any of these things using any of the 3 apis anymore. Please help! :)

-T
answered by trp (4.1k points)
0 votes
Hi

In order to fake any static call you need to use the type name as an argument to WhenCalled instead of the fake instance. This is because static methods belong to the type and not to instances of the type.

Example:
class ClassToFake
{
    protected static int NonPublicStaticMethod()
    {
        return 1;
    }

    public static int PublicStatic()
    {
        return NonPublicStaticMethod();
    }
}

[Test, Isolated]
public void Test()
{
    Isolate.NonPublic.WhenCalled(typeof(ClassToFake), "NonPublicStaticMethod").WillReturn(10);
    Assert.AreEqual(10, ClassToFake.PublicStatic());
}



:arrow: It is recommended not to mix the AAA api with older api.
:arrow: When you are using the AAA api especially on static calls use the Isolated attribute on the test class or test method. This will endure that the all fakes are cleared at the end of the test.
answered by ohad (35.5k points)
0 votes
in AAA how do I make an instance of a class?
answered by trp (4.1k points)
0 votes
Which one is AAA? Natural Mocks or Reflective Mocks?
answered by trp (4.1k points)
...