Right now I am working on a CQRS project, and I am using WCF as my web service technology. In this project I decided to challenge myself and make a full TDD project. The first big problem I found was to simulate a real web server, because I didnt want to hit a production or QA environment to run my tests.
Second I didnt want to write any integration test at all, so I had to figure out how to create an in-Process test to run my WCF methods.
Why in-process test? When you write a unit test, you should test a single functionality, and in order to make the test atomic, it should run in-process. It should not involve third party tiers like a database or IIS, otherwise the test wont be in-process anymore, so it will became an Integration test.
The first idea I came out was to fake the behaviour of IIS, and in order to accomplish this I could easily work with a mock framework like Typemock and mock out the entire IIS infrastructure.
Is this what I really want? Not at all. I dont want and I dont need to fake the behaviour of IIS, I need an instance of it because my WCF services are using custom factories and custom behaviours and I need to be sure they are acting in the right way.
So I have created two different type of tests. The first one is pure Unit Test, where I verify the command behaviours:
The second one is the (in)famous integration test, where you call the WCF service and test the overall process. Unfortunately for the second step I couldnt find anything really TDD so I had to figure out by myself a different solution.
Yesterday I spent some time googling around for a solution while I found a nice post from Scott Gu about IIS express. With IIS express we have an in-process IIS server that we can startup or shutdown at any time during the lifecycle of our tests. It provides almost all the functionalities provided by IIS and it also trigger the Application_Start event, something not very well handled by Cassini
So, first of all I had to figure out how to bootstrap my unit tests to run over a different IIS than the one configured in the web.config.
Piece of cake, first of all I need something that allows me to bootstrap IIS express before my unit test harness starts:
Then I need another utility to kill everything when my test harness is done:
At this point I can write any test in the following format:
I found this technique very useful because we work with TFS on Azure so my Build environment cannot be polluted at all with any third party components or by installing the WCF service under test in the build machine.
In the same way I found very quick to bootstrap IIS express for my tests, honestly they are faster than running on my local IIS 8.
IIS Express utility class
Below is the code I use to initialize IIS express, feel free to use it in your tests and let me know if you come up with a better solution than this one.