title: Net Core: How to Simple Unit Test Repository and Service with Database Rows
body: <p>I have a model in Entity Framework. I want to make sure that Application Service is reading the Generic Repository pointing to the database correctly. How would I test this in Xunit, I am kind of stuck ? Receiving the error below, maybe I need to rewrite code. Need to validate that App service can read example data row below in InMemorydatabase.</p>
  
  <p><strong>Model:</strong></p>
  
  <pre><code>public partial class Department
  {
      public int DepartmentId { get; set; }
      public string DepartmentCode { get; set; }
      public string DepartmentName { get; set; }
      .... plus additional
  }
  </code></pre>
  
  <p><strong>DTO:</strong></p>
  
  <pre><code>public class DepartmentDto
  {
      public int DepartmentId { get; set; }
      public string DepartmentCode { get; set; }
      public string DepartmentName { get; set; }
  }
  </code></pre>
  
  <p><strong>Base Repository from IRepository:</strong></p>
  
  <pre><code>    public async Task&lt;T&gt; GetAsync(int id)
      {
          return await Table.FindAsync(id);
      }
  
  **Constructor:**
  
      public BaseRepository(DbContext context)
      {
          _context = context;
          Table = _context.Set&lt;T&gt;();
      }
  </code></pre>
  
  <p><strong>Service: DepartmentAppService.cs</strong></p>
  
  <pre><code>    public async Task&lt;DepartmentDto&gt; GetDepartmentById(int id)
      {
          var department = await departmentRepository.GetAsync(id);
          var departmentDto = mapper.Map&lt;Department, DepartmentDto&gt;(department);
          return departmentDto;
      }
  
  **Constructor:**
  
     public DepartmentAppService(IRepository&lt;Department&gt; departmentRepository, IMapper mapper)
      {
          this.departmentRepository = departmentRepository;
          this.mapper = mapper;
      }
  </code></pre>
  
  <p><strong>Attempted Test Code:</strong></p>
  
  <pre><code>public async Task Get_DepartmentById_Are_Equal()
      {
          var options = new DbContextOptionsBuilder&lt;TestContext&gt;()
              .UseInMemoryDatabase(databaseName: "TestDatabase")
              .Options;
  
          var context = new TestContext(options);
  
          Mock&lt;IRepository&lt;Department&gt;&gt; departmentRepositoryMock = new Mock&lt;IRepository&lt;Department&gt;&gt;();
          Mock&lt;IMapper&gt; mapperMock = new Mock&lt;IMapper&gt;();
          Mock&lt;DepartmentAppService&gt; departmentAppServiceMock = new Mock&lt;DepartmentAppService&gt;(departmentRepositoryMock, mapperMock);
  
          context.Department.Add(new Department { DepartmentId = 2, DepartmentCode = "123", DepartmentName = "ABC" });
  
          var test = await departmentAppServiceMock.GetDepartmentById(5);
  
          Assert.Equal("123", test.DepartmentCode);
  </code></pre>
  
  <p><strong>Receiving Error: How to Fix?</strong></p>
  
  <pre><code>'Mock&lt;DepartmentAppService&gt;' does not contain a definition for 'GetDepartmentById' and no accessible extension method 'GetDepartmentById' accepting a first argument of type 'Mock&lt;DepartmentAppService&gt;' could be found (are you missing a using directive or an assembly reference?)  
  </code></pre>
  
  <p>Also have dependency injection in the program. Open to code being rewritten as required,</p>