title: Unit testing callback function Func<T> in RhinoMocks
body: <p>I have been trying to figure out how to cover my internal function that is used as call back when getting item from a cache. I have a CacheService that accepts key and Func. The Func is called when the item is not in cache.</p>
  
  <p>This sample code was based from anothe suggestion here in SO, but the cacheService.Get method always returns null and the WhenCalled was never called.</p>
  
  <p>I understand its debatable whethere I should cover this internal method, but for start im trying possible way to cover this without creating a new class just for this internal methods. Appreciated your help.</p>
  
  <p><strong>CacheService.cs</strong></p>
  
  <pre><code>    public T Get&lt;T&gt;(string key, Func&lt;T&gt; funcWhenNotExist)
      {
          if (!Exists(key))
          {
              var result = funcWhenNotExist();
              Cache.Put(key, result);
          }
  
          return DoSomethingWithRetry(() =&gt; (T)Cache.Get(key), MAXRETRY);
      }
  </code></pre>
  
  <p><strong>SomeService.cs</strong></p>
  
  <pre><code>    public SomeDto GetSomeDto(string dtoAlias)
      {
             string cacheKey = "whatever";
             var someDto = cacheService.Get(cacheKey, () =&gt; GetSomeDtoInternal(dtoAlias));
  
             return someDto
      }
  
      internal SomeDto GetSomeDtoInternal(string dtoAlias
      {
      //do more here
      }
  </code></pre>
  
  <p><strong>SomeServiceTest.cs</strong></p>
  
  <pre><code>    // Arrange
          SomeDto stubResult = new SomeDto();
          cacheService.Stub(s =&gt; s.Get(Arg&lt;string&gt;.Is.Anything, Arg&lt;Func&lt;SomeDto&gt;&gt;.Is.Anything)).WhenCalled(m =&gt;
          {
              var func = (Func&lt;SomeDto&gt;) m.Arguments[1];
              stubResult = func();
          }).Return(stubResult);
  
          // Act
          var result = service.GetSomeDto("whateveralias");
  </code></pre>