Unit testing is not something you add to the middle of existing methods, it is about testing small units of code in isolation from the rest of the system such that you have confidence that the unit is behaving as it should.
So, you should write a second class that's sole responsibility is to test that the class in which DoSomething lives (let's call this class Daddy and the test class DaddyTests) behaves as you expect it to. You can then write a test method that calls DoSomething and ensures that ErrorMessage is set appropriately (also ErrorMessage should be an out param, not ref unless you are also passing a value in).
To facilitate this test you will need to make sure that GetData returns no data. Trivially you can do this by passing in an empty set of data in a fake provider but in more complex scenarios whole classes may have to be swapped out for fake/mock equivalents: the use of interfaces and dependency-injection makes this task very simple. (Typically the provider is set during Daddy's construction, not as a parameter in the call to DoSomething.)
public class Daddy {
public List<MyClass> DoSomething(string Name, string Address, string Email, out string ErrorMessage, IDataProvider provider)
{
//Check for empty string parameters etc now go and get some data
List<MyClass> Data = provider.GetData(Name, Address, Email);
if (Data.Count == 0)
{
ErrorMessage = "Oh noes";
return Enumerable.Empty<MyClass>();
}
List<MyClass> formattedData = FormatData(Data);
return formattedData;
}
}
[TestClass]
public class DaddyTest {
[TestMethod]
public void DoSomethingHandlesEmptyDataSet() {
// set-up
Daddy daddy = new Daddy();
// test
IList<MyClass> result = daddy.DoSomething("blah",
"101 Dalmation Road",
"[email protected]",
out error,
new FakeProvider(new Enumerable.Empty<AcmeData>())); // a class we've written to act in lieu of the real provider
// validate
Assert.NotNull(result); // most testing frameworks provides Assert functionality
Assert.IsTrue(result.Count == 0);
Assert.IsFalse(String.IsNullOrEmpty(error));
}
}
}
ErrorMessageflag is a little bit awkward IMO.exceptionprobablyArgumentExceptionExceptionclass has a message property, puy the error message there.