If you want complete independence and isolation, you will likely have to expose some of the internal elements of MyHashTable
. For example, the base collection, which Add
adds elements to:
public class MyHashTable<TKey, TValue> { internal Dictionary<TKey, TValue> Storage { get; set; }
This is not very beautiful, as you can see, but, as I said, it is impossible to test these two methods in complete isolation without exposing anything (as you noticed, these are a couple of methods). Naturally, you can provide other ways to initialize your class, for example. with the constructor taking the collection, but it only delegates the problem one step further - you will need to verify that the constructor worked too, and you probably need the Get
method for this ... that returns you to the original problem.
Edit:
Please note that I do not propose to disclose internal / implementation details as the only way to the next. You can simply test these methods together (not as in one test, but using another), which may be the best solution. Of course - if your Add
fails at some point, the Get
tests won't work either. But then again, you want to fix the broken Add
- as soon as everything is over, everything will be in order. Naturally, the problem is how you differ from whether it was torn - t21> or Get
, but what if the correct error messages for the statements or the concepts of the protective statements that I linked in the comments come in handy.
True, sometimes complete separation and isolation are too complex to achieve or simply require you to introduce poor design. This is one of the reasons why they should never be the ultimate goal.
source share