title: How do I unit test protected properties meant to be set only by NHibernate?
body: <p>I'm using NHibernate to persist this entity:</p>
  
  <pre><code>public class Store
  {
      public int Id { get; protected set; }
      public int Name { get; set; }
  }
  </code></pre>
  
  <p>Note how the <code>Id</code> property has a protected setter. This is to prevent users from changing the <code>Id</code> while still allowing NHibernate to assign it an <code>Id</code> when it saves it to the database.</p>
  
  <p>In one of my unit tests, I'm using Moq with the following code to mock out my repository:</p>
  
  <pre><code>var mock = new Mock&lt;IRepository&gt;();
  mock.Setup(x =&gt; x.GetById&lt;Store&gt;(It.IsAny&lt;int&gt;()))
      .Returns(new Store { Value = "Walmart" }); // can't set the ID here
  
  var store = Repository.GetById&lt;Store&gt;(5);
  Assert.That(store.Id == 5);
  </code></pre>
  
  <p>When I tell Moq to return a new <code>Store</code> instance, I can't assign the ID, and the unit test fails. How do I unit test this property? I don't want to change the access level of the property because I don't want users to change it manually, and yet that's exactly what I have to do here in order to test it.</p>