I’m testing a method that calls another method within the same class.
Depending on whether this internal method call returns a result I do different things. In this case it’s a Cache class and I’m testing my GetOrStore method. I want to test two paths, when Get returns null and when Get returns something.
What’s the right approach to controlling the output of Get when called from GetOrStore? It feels like Get should be executed on a mocked instance however I’m unsure how to do that given two methods in the same class.
Update
The only thing I can think to do at this time is to make sure the cache key is removed before running the test:
HttpRuntime.Cache.Remove("foo"); // Make sure foo isn't in the cache.
var output = _cache.GetOrStore("foo", () => "Foo", 100);
Assert.AreEqual(output, "Foo");
This just feels wrong though. I don’t actually want the HttpRuntime cache touched during my test.
The answer to this is in Moqs
CallBaseproperty on mocked instances.Setting
CallBasetotrueensures that any methods not explicity setup are called on the real instance of the mocked object.This then means we can setup the
Getmethod and still haveGetOrStoreexecuted correctly: