Sunday, September 18, 2011

Mock When You Can


(forgive the bad iFrame)
This creepy footage of Woodrow Wilson drawing for who the first "lucky" draftee will be for the U.S. to step into World War I is not unlike tests that interact with live data. It's random. Who knows what you'll get back. You can fab up data and then destroy it, but what if you are just trying to check a row count and some preexisting local data is making a test break by throwing the count off? It's better to mock if you can. In the example below, FooRepository can be mocked.

[TestMethod]

public void GetAllFoo()

{

   var foo = TestObjects.BuildProgram("aNameForMyFoo");

   foo.Save();

   var foos = this.FooRepository.GetAll();

   Assert.IsTrue(foo.ToList().Count == 1);

}

 
 

Your tests are delicate and they need your love. Be good to them.


No comments:

Post a Comment