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
Hello,

I have recently implemented a Map (bi-directional dictionary) using two dictionaries as private members.

I would like to test the Clear() method on my data structure. However, that would require me to mock the private members. Visual Studio is unable to create private accessors to members.

Looking through your documentation, I found out that using reflective mocking I could create 2 dictionary mocks, and record the expected calls on them. The problem is such a test is not very readable: It is not clear which Dictionary instance within the class is associated with which mock. Also, the test is fragile as adding a third Dictionary to the class might break the tests.

Is there any better way of testing these private members without changing the interface of my Map class?
asked by mod (5.8k points)

1 Answer

0 votes
Hi,

Mocking is done according to the order you create the objects. If both are from the same class, the first one created will be connected to the first mock you created. Unless you use MockAll, which applies to all instances, this is how instance mocking works.

The kind of interaction you are testing, requires some "black-box" knowledge. You need to set up the expectations in the way you know how the inner code is written. So in that sense, you are correct - since the test code relies on knowledge of the inner code, it is fragile in that sense. But this is how interaction testing works - you need to know the interactions with other objects (and know which type they are and how many there are) in order to test correctly.

What you may be able to do is state-based testing. For example, test your map size after calling Clear, and see if it equals 0. This test code relies on the external interface only, and not on the internal implementation - it's more readable and less fragile.

Would you like to post the code and test (or send it to us privately) so we can understand better? We'll be glad to comment and help if we can.
answered by gilz (14.5k points)
...