I’m currently working on what I would call integration tests. I want to verify that if a WCF service is called it will do what I expect.
Let’s take a very simple scenario. Assume we have a contract object that we can put on hold or take off hold. Now writing the put on hold test is quite simple. You create a contract instance and execute the code that puts it on code.
The question I have comes when we want to test the taking off hold service call. The problem is that putting a contract on hold can be actually quite complicated leading to various objects all be modified. So usually I would use the Builder pattern and do something like this..
var onHoldContract = new ContractBuilder().PutOnHold().Build();
The problem I have with this is now I have to pretty much replicate a large part of my put on hold service. Now when I change what putting something on hold means I have two places I have to modify.
The other option that immediately jumps out at me is to just use the put on hold service as part of my test setup but now I’m coupling my test to the success of another piece of code which is something I don’t like to do since it can lead to failures in one spot breaking unrelated tests elsewhere (if put on hold failed for example).
Any other options I’m missing out here? or opinions on which method is preferable and why?
The Mock framework is a good option for unit testing. But for what I understood you are actually doing integration at this stage right? So you’re calling your WCF service as a black box (from a client point of view). I’m assuming that when you’re executing your On Hold operation, you have to do some persistance on a repository (Database, XML files, etc).
In this case then, the mock framework only at the testing side will not help you much, because in order to test the Off Hold operation, you need a On Hold object in the proper state, including repository entries etc. Seems to me that the only way to do this without having to reinvent the wheel is to use the service to put it On Hold first. But if you’re admant to decouple then, then you will need to setup the environment and this means code duplication (I actually had to do this on some integration test scenarios – what I would do was to setup it on the start of the test run).
Notice that your unit tests would be on a lower level, in the implementation of the service itself, and there you should be decoupling it as much as possible – this is where I would be applying the Mock Framework.
I hope this helps.