title: Mock (Moq) or override a readonly propery in a sealed class?
body: <p>I have a situation where in MS Dynamics Crm it returns some objects using sealed classes and readonly properties (I can only presume using internal constructors or internal property sets). And these guys don't inherit from an interface I can use. Obviously, if I had control over this code I would have a lot more control, but because it's in the underlying MS Dynamics framework I'm a bit stuck.</p>
  
  <p>The class I'd like to mock/override is called <a href="http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.aliasedvalue.aspx" rel="nofollow">AliasedValue</a>. The reason I want to mock it is I'm trying to simulate a call to dynamics and want to pretend I'm getting returned some Aliased Values.</p>
  
  <p>So here's the scenario, I want to create a sample entity that Dynamics will return... something like this:</p>
  
  <pre><code>var new_entity = new Entity("new_entity")
      {
          Attributes = new AttributeCollection
              {
                  {"new_name", "BR"},
                  {"createdon", DateTime.Now.AddDays(-1).Date},
                  {"new_field", "My field"},
                  {"new_contact", new AliasedValue {*****} }
              }
      };
  </code></pre>
  
  <p>So I am referencing a "contact" entity (in real time this comes back as an AliasedValue). While testing I want to make sure my code handles certain values that might come back in this field (e.g. it knows how to deal with an aliased value, and that if no linked contact is returned it doesn't blow up, etc etc). </p>
  
  <p>So if you've clicked on the link to AliasedValue you'll notice the properties are read only... so I can't set up test data...</p>
  
  <p>Next up, I was going to create my own class and override the whole thing... but it's sealed...</p>
  
  <p>And on top of this, as you probably have guessed, Moq doesn't like trying to mock a sealed class.</p>
  
  <p>I've read I'll need to purchase some "better" mocking framework to do this, but I really don't want to have to fork out some extra money just to get around this 1 thing.</p>
  
  <p>Anyone got a nice neat little solution for me to get around this?</p>
  
  <p><strong>Update for Clarification</strong></p>
  
  <hr>
  
  <p>A sample of how this is returned is as follows. I have a service which will be mocked to return the above object. Or what it actually returns is a collection containing the above object. So this is like so:</p>
  
  <pre><code>var service = new Mock&lt;IOrganizationService&gt;();
  service
      .Setup(s =&gt; s.RetrieveMultiple(null))
      .Returns(new EntityCollection (new List&lt;Entity&gt; {new_entity}));
  </code></pre>
  
  <p>This is great, I can return the above entity and do what I want with it. So, after I have created the above entity and I return it via my mocked service I get the above in my "EntityCollection". And once I have my entity I access the property as follows:</p>
  
  <pre><code>var aliasedContact = (AliasedValue)new_entity.Attributes["new_contact"];
  </code></pre>
  
  <p>Maybe I'm being silly but I can't quite get how the answer (by Lunivore) will solve this... (adding a comment to get feedback...)</p>