I have code similar to this:
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);
}
This is simplified a lot however I have tests that check the validity of the Expected contents when DataIn is called. However I now have realised I have to clear Expected after every DataIn call. This has broken my tests obviously as there is nothing in it to assert against.
Any suggestions on how to keep the tests that validate the contents but also allow me to empty the collection?
The answer is simple – your code does not pass your test. I.e. it does not behave as expected (you describe expected behavior in your test). So, code should change – remove call to Expected.Clear(). Or think about what you expect from your code.
UPDATE (for case when expectations changes):
Also consider doing behavior testing instead of state testing. By providing mocks to your class you can check how it interacts with them. I.e. check what data passed to your class dependency.