title: Moq: Mock SetUp method only returns null during test
body: <p>I just added dependency inject into my application, and would like to run mock test on my methods to ensure they are working properly.</p>
  
  <p>I have a domain class <code>Person</code> with the following properties:</p>
  
  <pre><code>public class Person : DomainBase
  {
     public string FirstName { get; set; }
      public string LastName { get; set; }
      public char Gender { get; set; }
      public DateTime DOB {get; set; }
  }
  </code></pre>
  
  <p>I have a partial <code>manager</code> class that is shared with other domain type mangers:</p>
  
  <pre><code>public partial class Manager : IManager1
  {
      private IHiveTiesContext _ctx; 
      public Manager(IHiveTiesContext context)
      {
          _ctx = context;
      }
  }
  </code></pre>
  
  <p>The interface <code>IManager1</code> was extracted from my <code>PersonManager</code> class and contains all need methods.</p>
  
  <p>My <code>PersonManager</code> <code>CreatePerson()</code> method is being called and tested:</p>
  
  <pre><code>public partial class Manager : IManager1
  {
      public Person CreatePerson(string fn, string ln, DateTime dob, char gender, Guid RId)
      {
         var _person =_ctx.People.Add(new Person
         {  
            FirstName = fn,
            LastName = ln,
            DOB = dob,
            Gender = gender,
            RowId = RId});
  
            _ctx.SaveChanges();
            return _person;
          }
      }  
  } 
  </code></pre>
  
  <p>My context follows and once again I extracted an interface <code>IHiveTiesContext</code> from it.</p>
  
  <pre><code>public class HiveTiesContext : DbContext, IHiveTiesContext
  {
      public HiveTiesContext() :  base("hiveties")
      {
          Database.SetInitializer&lt;HiveTiesContext&gt;(null);
      }
  
      public virtual IDbSet&lt;Person&gt; People { get; set; }
  }
  </code></pre>
  
  <p>Finally I am trying to test <code>CreatePerson</code> like this:</p>
  
  <pre><code>public class PersonManagerMockTest
  {
      private static Guid personGuid;
  
      [ClassInitialize]
      public static void Init(TestContext test)
      {
          personGuid = Guid.NewGuid();
      }
  
      [TestMethod]
      public void AddNewPerson()
      {
          //Arrange
          var mockDbSet = new Mock&lt;DbSet&lt;Person&gt;&gt;();
  
          var mockContext = new Mock&lt;IHiveTiesContext&gt;();
          mockContext.Setup(x =&gt; x.People)
              .Returns(mockDbSet.Object);
  
          var manager = new Manager(mockContext.Object);
  
          //Assert
          var _person = manager.CreatePerson("Winston", "Gabriel", DateTime.Now, 'M', personGuid);
  
          if(_person == null)
          {
              throw new Exception("NOT WORKING MAN!!!!");
          }
  
          var personid = mockContext.Object.People.Single(x =&gt; x.RowId == personGuid).Id;
  
          //Act
          mockDbSet.Verify(x =&gt; x.Add(It.IsAny&lt;Person&gt;()));
          mockContext.Verify(x =&gt; x.SaveChanges());
      }
  }
  </code></pre>
  
  <p>My problem is that my <code>Person</code> object is never created, that is, it always returns a <code>null</code> value and the exception is always thrown. I believe I am telling it to return a <code>Person</code> entity, but I am not sure where I am going wrong. This is my first time using <code>Moq</code> and running <code>Mock Unit Tests</code>. </p>
  
  <p>I appreciate any suggestions. Thank you.</p>