Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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#?

like image 514
RationalGeek Avatar asked Mar 24 '10 12:03

RationalGeek


People also ask

What is a link seam?

Link seams are ways of changing the behavior of a particular piece of code without editing that code in place by exploiting the way that files are linked together. With android, we do this via build variants. Link seams are useful for espresso tests, but shouldn't be used directly to put the app into a testable state.

What are seams in programming?

A seam is a place where you can alter behavior in your program without editing in that place.

What are seams in testing?

Test seams are a simple way of replacing or expanding source code in production parts of a program for test purposes. If, for example, the behavior of certain statements prevents tests from running, the unit test can replace them with suitable alternatives.


2 Answers

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.

like image 109
Jennifer Zouak Avatar answered Oct 05 '22 15:10

Jennifer Zouak


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 http://site.typemock.com/storage/feature-images/dateTime.png?__SQUARESPACE_CACHEVERSION=1252774490561

like image 26
Marc Gravell Avatar answered Oct 05 '22 16:10

Marc Gravell