我有類似下面的代碼:附加功能打破了我的測試 - 很簡單的邏輯太
public List<string> Expected = new List<string>();
public int SpecifiedID;
public DataIn(int ID, string Value)
{
Expected.Add(Value);
//if (ID == SpecifiedID)
//Expected.Clear();
//Need to add this but my tests that do something like
//CollectionAssert.AreEqual(new[] { "2", "4", "6", "8" }, logic.Expected);
//all fail as Expected is empty.
}
//Example Test
[Test]
public void NewTestFunction()
{
MyClass logic = new MyClass();
logic.SpecifiedID = 4;
logic.DataIn(1,"2");
logic.DataIn(2,"4");
logic.DataIn(3,"6");
logic.DataIn(4,"8");
//This will FAIL if Expected.Clear is added in the class.
CollectionAssert.AreEqual(new[] { "2", "4", "6", "8" }, logic.Expected);
}
這簡化了很多,但是我有檢查的Expected
內容的有效性時DataIn
被稱爲測試。但是我現在已經意識到我必須在每撥打DataIn
之後清除Expected
。這顯然打破了我的測試,因爲沒有任何內容可以反對。
關於如何保持驗證內容的測試的任何建議,但也允許我清空集合?
哪個測試?這是不負責任的。 – 2012-03-21 12:42:37
我有測試,如斷言CollectionAssert.AreEqual(新[] {「2」,「4」,「6」,「8」},邏輯.ExpectedValues);如果我在我的方法中添加Expected.Clear,則這些類型的測試失敗。 – Jon 2012-03-21 12:43:59
大多數單元測試框架具有在所有單元測試之間運行的設置/拆卸機制。 – jgauffin 2012-03-21 12:45:06