I was attempting to mock some windows management classes. I'm avoiding natural mocks for the moment.
I have been successfully mocking out classes, but problems arose when I was required to return an enumerator on a management collection. Without using natural mocks, is it possible to use 'expectandreturn' to expect a GetEnumerator call and return a mocked version of said enumerator? A colleague of mine did it with natural mocks, but again, I was wanting to accomplish this without natural mocks as a tool for understanding.
The specific classes in question are:
ManagementObjectSearcher
ManagementObjectCollection
ManagementObjectCollection.ManagementObjectEnumerator
the searcher returns the collection and the collection creates the enumerator. I am getting an exception when trying to use this code:
Dim managementobjectsearcherMock As Mock = MockManager.Mock(GetType(ManagementObjectSearcher))
Dim managementobjectcollectionMock As Mock = MockManager.Mock(GetType(ManagementObjectCollection))
Dim managementobjectcollectionenumeratorMock As Mock = MockManager.Mock(GetType(ManagementObjectCollection.ManagementObjectEnumerator))
managementobjectsearcherMock.ExpectAndReturn("Get", managementobjectcollectionMock)
managementobjectcollectionMock.ExpectAndReturn("GetEnumerator", managementobjectcollectionenumeratorMock)
This is the exception for the first call:
An unhandled exception of type 'TypeMock.TypeMockException' occurred in TypeMock.dll
Additional information:
*** Method Get in type System.Management.ManagementObjectSearcher has no matching overload that returns TypeMock.MockObject<System.Management.ManagementObjectCollection>.
Any help on this would be most appreciated.