I assume the cache is a third-party cache? If so, then I would not check . Otherwise, you are testing a different code.
If this caching is so important, you need to check it out, I would go with integration or acceptance . In other words, click on the page (s) / service (s) in question and check the contents in this way. By the very definition of what you want to test, it is not a unit test .
On the other hand, if the cache is the one you deployed, you can easily use unit test functionality. You might want to test verification-based testing to verify cache behavior, as adding / removing from cache has been added / removed from verification. Note the mocking about how to achieve this.
To test the behavior through Mock objects (or something similar), I would do the following - although your code will be different.
class Cacher { public void Add(Thing thing) {
To test the method described above, I will have a test that used mock Cacher . You need to pass this to the method at run time or inject the dependency into the constructor. From here, the test will simply verify that cache.Get(50) called. Not that the item is actually fetched from the cache. This checks for cache usage behavior, not that it actually caches / retrieves anything.
Then you can return to state testing for Cacher separately. For example, you add / remove items.
As I said, this can be excessive, depending on what you want to do. However, you seem pretty certain that caching is important enough to guarantee such testing. In my code, I try to limit the layout of objects as much as possible, although this sounds like a valid use case.