Unit Testing: Using a different method to validate a proven method is performed correctly

I am working on writing unit tests correctly, and I wonder if this is a bad method for checking the result of the test method by calling another method to check the result?

T. In the example below, I can only verify that the StoreObject method call was successful (that is, the object is cached) by calling FetchObject or the HasCachedObjects property, both of which contain logic that needs to be checked separately. What would you do in this case when the result is hidden from the public API?

I have a Cache class:

public class Cache {

  private Dictionary<string, object> _Cache = null;

  public bool HasCachedObjects {
    get {
      if (_Cache.Count > 0) {
        return true;
      } else {
        return false;
      }
    }
  }

  public Cache() {
    _Cache = new Dictionary<string,object>();
  }

  public void StoreObject(string key, object obj) {
    _Cache[key] = obj;
  }

  public object FetchObject(string key) {
    if (_Cache.ContainsKey(key)) {
      return _Cache[key];
    }

    return null;
  }
}
+3
source share
3 answers

; (, , ).

, StoreObject_WhenSuccessful_AddsToCache .

, API.

+3

. , , , - , HasCachedObjects , - , .

( , , API, ...)

+2

/, : - , ..

0
source

Source: https://habr.com/ru/post/1724576/


All Articles