0

I've created a custom equality comparer, and ran the appropriate tests against the comparer.

Now I'm attempting to mock it out so that the class that I am testing that uses the comparer doesn't have to pass in the real thing.

My method call looks like:

left.Except(right, customEqualityComparer);

Where the variables 'left' and 'right' are two lists of any type and customEqualityComparer is my custom equality comparer.

My problem is that I do not know how to mock customEqualityComparer as it has to still act in a realistic way.

I was considering creating a class that contains the method:

public List<object> LeftExceptRight(customEqualityComparer, left, right){}

And mocking the whole method. Is this overkill? I can't see any other alternatives..

SamuelDavis
  • 3,312
  • 3
  • 17
  • 19
  • Say your comparer is `CustomComparer`, does your class have a direct dependency (ie call `new CustomComparer()`) or is getting it through an interface (`IComparer`) ? – Simon Belanger Jul 02 '13 at 21:54

1 Answers1

1

Is this overkill?

Sounds like it to me. Why do you want to mock your comparer in the first place? Is it doing anything particularly difficult in terms of testing, such as using external web services or a database? (That would be unusual.)

So long as your comparer is also tested thoroughly in itself, I see no problem with using it in the tests for whatever will use it in reality. You don't get any significant benefit in mocking it - quite the reverse, actually. When you use it in your tests, you'll be effectively testing the integration between the calling code and the comparer as well... that they work well together, not just alone.

I know there are those who dogmatically insist that unit tests really need to only test your code from one class at a time... but pragmatism has led me to believe that while mocking is really valuable in many cases, it's easy to go overboard with it - and that the simple approach of just using the real implementation is often the way to go.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • The CustomEqualityComparer will only ever be used for left.except(right), right.except(left) and list1.Intersect(list2). Do you think it would warrant it's own testing if I were to create a wrapper class for the 3 methods taking the CustomEqualityComparer in the constructor? This way the whole module would be completely pluggable. – SamuelDavis Jul 02 '13 at 22:12
  • 1
    @SamuelDavis: I dare say - but that doesn't mean you can't use it in the tests for the other class. What benefit do you think you'd derive from mocking it out? – Jon Skeet Jul 02 '13 at 22:12
  • Sorry, accidentally pressed 'enter' while only typing out half my comment. Could you re-read my initial reply? If I were to have a little tested 'custom list extension' class it could be used in more projects. – SamuelDavis Jul 02 '13 at 22:14
  • @SamuelDavis: Do you really *need* it to be pluggable? Would you be extracting those three methods just for the sake of this testing? Try not to bend your production code completely out of shape just so you can mock a dependency which really doesn't need to be mocked. – Jon Skeet Jul 02 '13 at 22:18
  • I created a dynamic equality comparer, which takes in a hard coded list of property names to compare against. It would almost certainly be used elsewhere soon, which is why I went to the effort of thoroughly unit testing the comparer separately. – SamuelDavis Jul 02 '13 at 22:20
  • 1
    @SamuelDavis: If you want to extract the collection code out separately, then sure, do so - but you don't need to *mock* the comparer. I typically use a case-insensitive (but otherwise ordinal) string comparer for that purpose - it's easy to demonstrate equal but non-identical data values. – Jon Skeet Jul 02 '13 at 22:21
  • I've got my dynamic comparer on stack overflow at 'http://stackoverflow.com/questions/17015566/how-do-you-create-a-dynamic-equality-implementation-where-you-can-pass-in-the-pr' if You've got any ideas. Always happy to see my code criticized. – SamuelDavis Jul 02 '13 at 22:28
  • But thanks for the great answer, it's given me some great ideas. PS. was just watching your tekpub screencasts last week, you've been a huge inspiration. – SamuelDavis Jul 02 '13 at 22:30