title: How to Mock a readonly property whose value depends on another property of the Mock
body: <p>(As indicated by the tags, I am using moq).</p>
  
  <p>I have an interface like this:</p>
  
  <pre><code>interface ISource
  {
    string Name { get; set; }
    int Id { get; set; }
  }
  
  interface IExample
  {
    string Name { get; }
    ISource Source { get; set; }
  }
  </code></pre>
  
  <p>In my application, concrete instances of IExample accept a DTO (IDataTransferObject) as the Source.  Some properties on the concrete implementation of IExample are simply delegated to the Source.  Like this...</p>
  
  <pre><code>class Example : IExample
  {
    IDataTransferObject Source { get; set; }
  
    string Name { get { return _data.Name; } }
  }
  </code></pre>
  
  <p>I would like to create a standalone mock of IExample (standalone meaning that I cannot use a captured variable because several instances of the IExample mock will be created in the course of a test) and setup the Mock such that IExample.Name returns the value of IExample.Source.Name.  So, I would like to create a mock something like this:</p>
  
  <pre><code>var example = new Mock&lt;IExample&gt;();
  example.SetupProperty(ex =&gt; ex.Source);
  example.SetupGet(ex =&gt; ex.Name).Returns(what can I put here to return ex.Source.Name);
  </code></pre>
  
  <p>Essentially, I want to configure the mock to return, as the value of one property, the value of a property of a subobject of the mock.</p>
  
  <p>Thanks.</p>