如果你写了一种方法的话,那就是一种方法。
public static string ToMappingString(this Widget obj)
然后,你可以通过批准测试(www.approvaltests.com或nuget)轻易测试。
There s a video here: http://www.youtube.com/watch?v=vKLUycNLhgc
However, if you are looking to test "My objects save and retrive themselves"
Then this is a perfect place of "Theory Based Testing"
Theory based testing
Most unit test take the form of
Given A,B expect C
理论测试
Given A,B expect Theory
无需担心哪一种特殊形式的A & B就算,因为你不需要知道C,所以任何随机发电机都会工作。
例1:测试添加和分流方法
正常情况下,你会遇到同样的问题。
Assert.AreEqual(5, Add(2,3));
Assert.AreEqual(9, Add(10,-1));
Assert.AreEqual(10, Add(5,5));
Assert.AreEqual(7, Subtract(10,3));
但是,如果你写了字句的话,你就写了字。 它希望测试。
for(int i = 1; i < 100; i++)
{
int a = random.Next();
int b = random.Next();
Assert.AreEqual(a, Subtract(Add(a,b),b, string.Format("Failed for [a,b] = [{0},{1}], a,b));
}
现在你们理解 理论检验,你试图测试的理论
Given Model A
When A is stored to the database, and retrieved the resulting object is equal to A