views:

53

answers:

2
+3  Q: 

Link seams in .NET

I just recently finished Michael Feathers' book Working Effectively with Legacy Code. It was a great book on how to effectively create test seams and exploit them to get existing code under test.

One of the techniques he talk about was using "link seams". Basically the idea was that if you had code that depending on another library you could use the linker to insert a different library for testing than for production. This would allow you to sense test conditions through a mock library, or avoid calling into libraries that have real world effects (databases, emails, etc.), etc.

The example he gave was in C++. I'm curious if this technique (or something similar) is possible in .NET / C#?

+1  A: 

That sounds something a bit like the things Typemock isolator offers, in particular their claimed ability to rip out and mock existing types. But I've never used it ;-(

As an example, DateTime.Now is something that shouldn't be mockable, right? alt text

Marc Gravell
+1  A: 

Yes it is possible in .Net. In the simplest case, you can just replace an assembly with another one of the same name.

With a strongly named assembly, you should change the version number and then configure assembly bindings to override the compile time "linked" version. This can be done on an enterprise, machine, user or directory level.

There are some caveats, related to security. If the assembly you wish to substitute has been strongly named, then you will need to recreate the same public key in signing the assembly.

In other words, if you as the application developer do not want your libraries "mocked" (or perhaps replaced with malicious code) then you must ensure that the assembly is signed and the private key is not publicly available.

That is why you cannot mock DateTime -- because Microsoft has strongly named the core libraries of .Net.

Jennifer Zouak