8

I have an application that references a Microsoft DLL (Exchange Web Services). In my unit testing, I want to replace the Microsoft DLL with a Mock.

What works with other DLLs, doesnt work with this DLL, as i get an exception

The located assembly's manifest definition does not match the assembly reference

I verified it has the same assembly version, but i saw that the Microsoft DLL is signed digitally, so it has a PublicKeyToken. Could this be what my Application is looking for? A similarly signed DLL?

Is there a way of referencing a DLL without requiring its publickeytoken?

Thanks

starblue
  • 55,348
  • 14
  • 97
  • 151
Yiftach
  • 81
  • 2
  • 1
    How about: msvcr71d.dll is so ugly that even rundll32.exe says it has a bad image!!! Right? Right? I'm so sorry... – diceguyd30 Mar 15 '11 at 20:11

1 Answers1

13

You can't mock a signed DLL. (If you could you will be hired by mafia...)

Mock the code that accesses code in this dll.

Create a facade (if you don't have it), that wraps access to methods in Exchange dll. Then in your tests you can provide a mock facade.

It is a good design practice to shield your code from any third-party code (assemblies, web services, control) by the means of facades / wrappers. This minimizes the risk of impact should the third party code change, ie. upgrading to a newer version of dll (the required changes will affect only the facade) and help with testing.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
  • You can mock system dll's with some trickery, for instance PEX does this. – oɔɯǝɹ Mar 15 '11 at 19:59
  • @remco - Are you referring to http://research.microsoft.com/en-us/projects/moles/? – Jakub Konecki Mar 15 '11 at 20:03
  • This doesn't actually work as you have to pass a Server instance in to most of the methods. – Nathan C. Tresch Oct 29 '13 at 03:43
  • @NathanC.Tresch - What do you mean by 'pass a Server instance in to the method'? – Jakub Konecki Oct 29 '13 at 08:44
  • @JakubKonecki A great many of the Exchange Service utility methods are used by passing an instantiated ExchangeService into them. You can't pass an adapter or facade in it's place, so this technique doesn't actually work for the asker's specific situation. – Nathan C. Tresch Nov 05 '13 at 03:08
  • @NathanC.Tresch The whole point of the facade is to hide the `ExchangeService` class so the consumer of the facade doesn't have to be aware of it. It is facade's responsibility to instantiate `ExchangeService` class and then pass the instance to whatever Exchange Service utility methods it needs. That way you shield your code from depending on Microsoft classes. – Jakub Konecki Nov 05 '13 at 09:41