So I have something along the lines of this
private List<ConcurrentQueue<int>> listOfQueues = new List<ConcurrentQueue<int>>()
public void InsertInt(int id, int value)
{
listOfQueues[id].Enqueue(value);
}
Is this something I shouldn’t be unit testing?
If it is how do I test the InsertInt method without using any other methods?
is it ok to use the method to retrieve the data from the queue to test if the it was enter correctly?
Is this something I should be using mocks for?
You generally do not want to test private members. The point of unit testing is that from the outside, the class takes the specified inputs and (when requested) will give you the correct outputs. You don’t care about the implementation of HOW it gives you those outputs, you just care that it gives you the correct outputs for external usage.
To show this, say you create a unit test verifies that your
InsertInt()method inserts the int tolistOfQueues. Then your requirements change, and you have to change your implementation strategy, and instead of using aList<ConcurrentQueue<int>>it becomes aDictionary<string, ConcurrentQueue<int>>. This may not actually require a change to your inputs, and your outputs can still pass any output verification, but yourInsertInt()unit test will fail because it’s hard coded to the implementation.The better idea is to do unit tests that makes sure that if you call
InsertInt()with the correct input, that your output methods will still return the corrected output, as well as creating unit tests that callingInsertInt()with invalid parameters causes exceptions. You can then change everything about your internal implementation and be confident that your class is still working correctly. Unit testing the implementation adds extra over-head while providing very little benefit in testability.Note that I am NOT saying that this method should not be unit tested, it’s just that the unit tests need to be developed in a way that reflects how outside objects will interact with your class.