title: Cannot instantiate proxy of class error
body: <p>I am doing a simple unit test where when creating a Course, the Title field cannot be empty. I am having to test it with a service class that has Dependency Injection with UnitOfWork. When I debug my test, I am getting an Exception error of <code>Can not instantiate proxy of class: ContosoUniversity.Models.CourseRepository</code> I looked into the error, but am not able to understand how to fix the issue and the Assert statement?</p>
  
  <p><a href="https://i.stack.imgur.com/gp18F.png" rel="noreferrer">Error Message Display Image</a></p>
  
  <p><strong>CourseRepository</strong></p>
  
  <pre><code>public class CourseRepository : GenericRepository&lt;Course&gt;
  {
      public CourseRepository(SchoolContext context)
          : base(context)
      {
      }
  </code></pre>
  
  <p><strong>UnitOfWork</strong></p>
  
  <pre><code>public class UnitOfWork : IDisposable, IUnitOfWork
  {
      private SchoolContext context = new SchoolContext();
      private GenericRepository&lt;Department&gt; departmentRepository;
      private CourseRepository courseRepository;
  
      public CourseRepository CourseRepository
      {
          get
          {
              if (this.courseRepository == null)
              {
                  this.courseRepository = new CourseRepository(context);
              }
              return courseRepository;
          }
      }
  
      public virtual CourseRepository GetCourseRepository()
      {
          if (this.courseRepository == null)
          {
              this.courseRepository = new CourseRepository(context);
          }
          return courseRepository;
      }
  </code></pre>
  
  <p><strong>CourseService</strong></p>
  
  <pre><code>public class CourseService : ICourseService
  {
      private IUnitOfWork unitOfWork;
  
      public CourseService (IUnitOfWork unitOfWork)
      {
          this.unitOfWork = unitOfWork;
      }
  
      public void Create(Course course)
      {
          unitOfWork.GetCourseRepository().Insert(course);
          unitOfWork.Save();
      }
  
      public Course GetCourseByID(int id)
      {
          return unitOfWork.GetCourseRepository().GetByID(id);
      }
  </code></pre>
  
  <p><strong>TestMethod</strong></p>
  
  <pre><code>[TestMethod]
  public void TestMethod1()
  {
      var course = new Course
      {
          CourseID = 2210,
          Title = string.Empty,
          Credits = 3,
          DepartmentID = 1
      };
  
      Mock&lt;CourseRepository&gt; mockRepo = new Mock&lt;CourseRepository&gt;();
      mockRepo.Setup(m =&gt; m.GetByID(course.CourseID)).Returns(course);
  
      var mockUnit = new Mock&lt;IUnitOfWork&gt;();
      mockUnit.Setup(x =&gt; x.GetCourseRepository()).Returns(mockRepo.Object);
  
      var myService = new CourseService(mockUnit.Object);
      myService.Create(course);
  
      //var error = _modelState["Title"].Errors[0];
      //Assert.AreEqual("The Title field is required.", error.ErrorMessage);
  
      //mockRepo.Setup(x =&gt; x.Insert(course));
  }
  </code></pre>