I’m working on a legacy system that has uses stored procs, business objects and DTO:s. The business objects and the DTO:s often have the same properties. When calling a method in the service layer that returns a DTO, many transformations are happening. Stored proc -> dataset -> business object -> DTO. If a new property is added, it sometimes happens that a developer forgets to add code that moves it from one layer/object to another.
In some parts of the system I solved this by using AutoMapper which will automatically project properties with the same name.
My question is for the other parts. Can I somehow write a unit test that checks if every property in an object has been set/given a value? That way I could write an integration test that calls our service layer and all the transformations have to be successful for the test to pass.
I guess the solution would involve reflection.
Reflection is one way, but it has its caveats, if you set a property to its default value, you will not pick up on the fact it was set.
You can intercept with a real proxy and then listen on all property changes. See the code here for a base interceptor you can use. Note interceptors mean you need your object to be MarshalByRefObject which may not be something you want. So the other option is to tell your factory to wrap up the object before it returns it in the test scenario. Something that ninject or many other inversion of control libs will allow you to do.