I have a class method that looks like this:
private List<string> DataStoreContents = new List<string>(new[] { "", "", "", "" });
public void InputDataStore(int DataStore, string Data)
{
DataStoreContents[DataStore - 1] = Data;
}
I want to make sure that DataStore is >=1 and <= 4
How can I write a unit test that ensures that?
Either
or, if you prefer the fluent interface
[EDIT – in response to you clarification above]
It sounds like you want to have some sort of barrier checking to check that the supplied values are in range.
In this case, you have a few choices:
Philip Fourie has given an answer involving code contracts.
Another simple approach is to write the barrier check yourself:
If you don’t want to throw an exception, but maybe want to log it and exit cleanly:
To link back to unit testing. A unit test, for example, could be a test you write to check that when
InputDataStoreis called with a value that is out of range, that it throws an expcetion. Another would be that when it is called with a value in range, it doesn’t throw an exception, and it updatesDataStoreContentscorrectly.