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
I am preparing a typemock test case for an internal class. However when trying to verify a private method of this internal class, I am getting the following exception.

TypeMock.VerifyException:
TypeMock Verification: Method Components.Workspace.Impersonation.ImpWorkspaceProvider_Accessor.CreatePermissions(Microsoft.SharePoint.SPWeb, Microsoft.SharePoint.SPWeb) was expected but was not called.

The method is actually getting called and executed.

Following is the code.

     [TestInitialize]
        public void TestInitialize()
        {
            _classUnderTest = Isolate.Fake.Instance<ImpWorkspaceProvider_Accessor>(Members.CallOriginal);
        }

[TestMethod()]
        [Isolated]
        [VerifyMocks]
        public void AddSiteTest()
        {
         //Arrange statements....
          Isolate.Fake.StaticMethods<HiddenList>(Members.CallOriginal);

          _classUnderTest.ReturnItem = fakeItem;            
          _classUnderTest.AddSite();

         Isolate.Verify.NonPublic.WasCalled  (_classUnderTest, "CreatePermissions"); //fails
         Isolate.Verify.WasCalledWithAnyArguments(() =>  _classUnderTest.CreatePermissions(null, null));//fails

         Isolate.Verify.WasCalledWithExactArguments(() =>HiddenList.GetSiteNameFromTitle(_cwsTitle)); //passes
}


The AddSite method inturn calls the CreatePermissions private method.

How can I verify this method call successfully? I am able to verify static method calls within the same method succesfully.
asked by anisree (760 points)

10 Answers

0 votes
Hi,

The VerifyMocks attribute belongs to older API that should not be mixed with the new AAA API.
Try to remove the VerifyMocks attribute (but leave the Isolated attribute) and see if it solves the problem.

Please let me know if it helps.
answered by ohad (35.4k points)
0 votes
Hi,
I tried removing the verifyMocks attribute. Still no luck. Any other suggestions?
answered by anisree (760 points)
0 votes
Hi,

I couldn't reproduce but it may relate to the private accessor generated by MSTest. Isolator can replace part of it using the private API (like Isolate.Invoke.Method).

You can write the test without using the accessors:

[TestInitialize]
public void TestInitialize()
{
    _classUnderTest = Isolate.Fake.Instance<ImpWorkspaceProvider>(Members.CallOriginal);
}

[TestMethod]
[Isolated]
public void AddSiteTest()
{
    Isolate.Fake.StaticMethods<HiddenList>(Members.CallOriginal);

    _classUnderTest.ReturnItem = fakeItem;
    _classUnderTest.AddSite();

    Isolate.Verify.NonPublic.WasCalled(_classUnderTest, "CreatePermissions");
    Isolate.Verify.WasCalledWithAnyArguments(() => _classUnderTest.CreatePermissions(null, null));

    Isolate.Verify.WasCalledWithExactArguments(() => HiddenList.GetSiteNameFromTitle(_cwsTitle));
}


Please let me know if it helps.

Regards,
Elisha
Typemock Support
answered by Elisha (12k points)
0 votes
Hi Elisha,
Thank you for the comment. But without the accessor I am not able to access the class itself since it is internal to the assembly. The typemock code definitely resides in a different assembly hence I had to create a private acceesor for the class. Are you suggesting that we will have to set the class to public to prepare this test case or am I missing something here?

Thanks
Anisree
answered by anisree (760 points)
0 votes
Hi,

In this case a very common pattern is to expose the tests assembly to internal members.

It can easily be done by using InternalsVisibleTo attribute to the assembly info.

For example, you can place in the assembly under test the next attribute (in AssemblyInfo.cs):
[assembly: InternalsVisibleTo("TestsAssembly")]

This way you can access all Internal members inside TestsAssembly without accessors. It's easier to maintain since changes in the assembly under tests are reflected right away in the test code.

Best Regards,
Elisha
Typemock Support
answered by Elisha (12k points)
0 votes
Hi Elisha,
I am already doing this (while generating the unit test case it added the attribute to AssemblyInfo.cs as well as created the accessor). However even after this I need the accessor reference for the class to access it directly from the Typemock project.
e.g: If I instantiate like this

[TestInitialize]
        public void TestInitialize()
        {
            _classUnderTest = new ImpWorkspaceProvider(); 
        }  

It will throw me error that ImpWorkspaceProvider is not accessible due to its protection level.

Thanks
Anisree
answered by anisree (760 points)
0 votes
Hi,

Is the constructor of ImpWorkspaceProvider private/protected?

If so it can be solved by using fake:
Isolate.Fake.Instance<ImpWorkspaceProvider>(Members.CallOriginal, ConstructorWillBe.Called);


Regards,
Elisha
Typemock Support
answered by Elisha (12k points)
0 votes
In this case the constructor should be internal. (not defined explicitly).
I tried your code, but still the verify call is failing.

I am using Isolator version 5.3. Hope this has nothing to do with this behavior.

Thanks
Anisree
answered by anisree (760 points)
0 votes
Hi there.

I use MSTest for unit testing and can mock objects which have an accessor class.

 <TestMethod()> _
    Public Sub DoTest()
        Dim fakeMyClass As MyClass = FakeInstance(Of MyClass)()

        Using TheseCalls.WillReturn(False)
            Dim dummy As Boolean = fakeMyClass.DoSomething
        End Using

        IsolateVB.NonPublicWillReturn(GetType(fakeMyClass ), "BuildDocumentFolderPath", "blah")

        SwapNextInstance(Of MyClass)(fakeMyClass)

        Dim target As New MyClass_Accessor '// This is set to fakeMyClass.

        target.PrivateMethod()

    End Sub


I'm not sure about the IsolateVB.NonPublic bit, I think that's how you use it. The idea is that you isolate the MyClass behaviour (no need to fake the accessor class) and the SwpNextInstance takes care of the rest. Even though the test code calls:

Dim target As New MyClass_Accessor 


it get's swapped with the fakeMyClass. I use this technique all the time.

Hope this helps.
Jas.
answered by MrClyfar (5.2k points)
0 votes
Hi Anisree,

Let's take it offline, I would like to check why internals are not exposed to the tests.

Regards,
Elisha
Typemock Support
answered by Elisha (12k points)
...