title: How to write unit tests for proxy pattern?
body: <p>Will be thankful for your attention, time and efforts ! <br />
  I have the following code</p>
  
  <pre><code>public class Employee
  {
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string Role { get; set; }
  }
  
  public interface IEmployeeRepository
  {
      Employee GetEmployee(string firstName, string role);
  }
  
  public class EmployeeRepository : IEmployeeRepository
  {
      public Employee GetEmployee(string firstName, string role)
      {
          //logic here
          return new Employee();
      }
  }
  </code></pre>
  
  <p>Now i want to implement cache for <code>EmployeeRepository</code>.
  At first i did it using Proxy design pattern</p>
  
  <pre><code> public class ProxyEmployeeRepository : IEmployeeRepository
  {
      private EmployeeRepository _employeeRepository = new EmployeeRepository();
      private MemoryCache _cache = new MemoryCache("UsualCache");
      public Employee GetEmployee(string firstName, string role)
      {
          //do not cache administrators
          if (role == "admin")
          {
              return _employeeRepository.GetEmployee(firstName, role);
          }
          else
          {
              //get from cache  at first
              //if absent call _employeeRepository.GetEmployee and add to cache
              //...
          }
      }
  </code></pre>
  
  <p>But when wanted to write unit tests for this class i couldn't do it(i cannot create mock for _employeeRepository  and verify whether it was called or not) <br/>
  If i implement cache with Decorator pattern then i would have the following  code</p>
  
  <pre><code> public class DecoratorEmployeeRepository : IEmployeeRepository
  {
      private IEmployeeRepository _employeeRepository;
      public DecoratorEmployeeRepository(IEmployeeRepository repository)
      {
          _employeeRepository = repository;
      }
  
      private MemoryCache _cache = new MemoryCache("UsualCache");
      public Employee GetEmployee(string firstName, string role)
      {
          //do not cache administrators
          if (role == "admin")
          {
              return _employeeRepository.GetEmployee(firstName, role);
          }
          else
          {
              //get from cache  at first
              //if absent call _employeeRepository.GetEmployee and add to cache
              return null;
          }
      }
  }
  </code></pre>
  
  <p>and unit tests for it</p>
  
  <pre><code> [TestClass]
  public class EmployeeRepositoryTests
  {
      [TestMethod]
      public void GetEmployeeTest_AdminRole()
      {
          var innerMock = Substitute.For&lt;IEmployeeRepository&gt;();
          var employeeRepository = new DecoratorEmployeeRepository(innerMock);
          employeeRepository.GetEmployee("Ihor", "admin");
          innerMock.Received().GetEmployee(Arg.Any&lt;string&gt;(), Arg.Any&lt;string&gt;());
      }
  
      [TestMethod]
      public void GetEmployeeTest_NotAdminRole()
      {
          var innerMock = Substitute.For&lt;IEmployeeRepository&gt;();
          var employeeRepository = new DecoratorEmployeeRepository(innerMock);
          employeeRepository.GetEmployee("Ihor", "NotAdmin");
          innerMock.DidNotReceive().GetEmployee("Ihor", "NotAdmin");
      }
  }
  </code></pre>
  
  <p><strong>Is it possible to write unit tests for first approach with proxy pattern ? i just don't understand how it is possible to cover proxy class with unit tests ...</strong></p>