I'm not very experienced in test-driven development (TDD), but I'm trying to get into the groove. Here I am making a console app that creates records in a database. I bootstrapped by creating a console app and then let MSTest make a unit test for Main(string [] args)
.
I know I'm doing at least something right, because the tests didn't pass until I did the development -- they "drove" me to "develop".
But I feel like maybe I'm still missing the point, as I didn't really test my code - I tested the database the code acted on. How would an experienced TDDer approach this?
[TestClass]
public class ProgramTest
{
private MyEntities _model;
[TestInitialize()]
public void MyTestInitialize()
{
_model = new MyEntities();
}
[TestCleanup()]
public void MyTestCleanup()
{
_model.Dispose();
}
[TestMethod()]
public void MainTest_01_Create_PayTerms()
{
string[] args = new[] { "/Operation", "Insert", "/RecordType", "PayTerms" };
DirectAgentsService.Program.Main(args);
_model.PayTerms.SelectValue<string>("it.Name").ShouldContain("Biweekly", "Retainer", "Net 7", "Net 15", "Net 30");
}
}
static public class Extensions
{
static public void ShouldContain(this string[] source, params string[] targets)
{
foreach (var target in targets)
{
Assert.IsTrue(targets.Any(c => c == target), "{0} is missing from collection", target);
}
}
static public void ShouldContain<T>(this T source, params string[] targets) where T : ObjectQuery<string>
{
source.ToArray().ShouldContain(targets);
}
}