Unit testing is not something that you add to the middle of existing methods, namely testing small units of code in isolation from the rest of the system so that you can be sure that the device behaves as it should.
So, you should write a second class, which should only answer to the fact that the class in which DoSomething lives (let this class be DaddyTests Daddy and the test class DaddyTests ) behaves as you expect. Then you can write a test method that calls DoSomething and ensures that ErrorMessage set appropriately (also ErrorMessage should be out param, not ref , if you don't pass the value to either).
To facilitate this test, you need to make sure that GetData not returning data. Trivially, you can do this by passing an empty dataset to a fake provider, but in more complex scenarios all classes may have to be swapped for fake / mock equivalents: using interfaces and dependency injection make this task very easy. (Normally, the provider is set during Daddy , not as a parameter in a DoSomething call.)
public class Daddy { public List<MyClass> DoSomething(string Name, string Address, string Email, out string ErrorMessage, IDataProvider provider) {
}
source share