title: Unit Testing Service with Unit of Work with xunit .NET Core 3.1 and Moq
body: <p>being fairly new to xUnit, I have an application that uses the Unit of Work pattern to return data to a service and I'm trying to unit test this service.</p>
  
  <p>Here is the service code:</p>
  
  <pre><code> public async Task&lt;IEnumerable&lt;CatDto&gt;&gt; GetActiveCatsAsync()
      {
          var catList = new List&lt;CatDto&gt;();
          var catEFList = await _uow.Repository&lt;Cat&gt;().GetActiveCatsAsync();
          catList.AddRange(catEFList.Select(c =&gt; new CatDto
          {
              CatId = c.CatId,
              CatName = c.CatName,
              DisplayOrd = c.DisplayOrd
          }));
  
  
          return catList;
      }
  </code></pre>
  
  <p>And here is the extension that this service calls:</p>
  
  <pre><code> public static async Task&lt;List&lt;Cat&gt;&gt; GetActiveCatsAsync(this IRepository&lt;Cat&gt; repository)
      {
          return await repository.AsQueryableRepo().Items.Where(c =&gt;c.IsActive == true).OrderBy(c=&gt;c.DisplayOrd).ToListAsync();
      }
  }
  </code></pre>
  
  <p>Here is the Interface used by the extension:</p>
  
  <pre><code> internal static IQueryableRepository&lt;T&gt; AsQueryableRepo&lt;T&gt;(this IRepository&lt;T&gt; repository) where T : class
      {
          return (IQueryableRepository&lt;T&gt;)repository;
      }
  </code></pre>
  
  <p>Here is the concrete unit of work class:</p>
  
  <pre><code> public class SQLUnitOfWork&lt;TContext&gt; : BaseUnitOfWork, ISQLUnitOfWork&lt;TContext&gt;
      where TContext : IDbContext, IDisposable
  {
      public SQLUnitOfWork(TContext context) : base(context)
      {
      }
  }
  </code></pre>
  
  <p>And here is where I am with a single unit test so far:</p>
  
  <pre><code>  [Fact]
      public void GetActiveCatsAsync_ReturnsTypeIEnumerableCatDto()
      {
          var mockLogger = new Mock&lt;ILogger&lt;CatService&gt;&gt;();
          var mockUoW = new Mock&lt;ISQLUnitOfWork&gt;();
  
          var catServ = new CatService(mockLogger.Object, mockUoW.Object);
  
          var result = catServ.GetActiveCatsAsync();
  
          Assert.IsType&lt;Task&lt;IEnumerable&lt;CatDto&gt;&gt;&gt;(result);
      }
  </code></pre>
  
  <p>This test actually passes, but obviously the "result" variable has no data in it because there is no setup for the mocks. I also tried setting up:</p>
  
  <pre><code> var mockRepo = new Mock&lt;IRepository&lt;Cat&gt;&gt;();
  </code></pre>
  
  <p>But this doesn't allow me to get at the extensions methods above. It doesn't show me the "asQueryableRepo()" method to provide a return value for. So I'm not sure how I can mock the return data, which would be a simple Cat object with three properties shown in the service call. I have also tried using xUnit's .ReturnAsync() and Task.FromResult() functionality.</p>
  
  <p>After a few searches, I tried implementing xUnit.DependencyInjection, but that requires configuration in the test project's Startup.cs file. My test project (created with the Visual Studio xUnit project template) doesn't have a Startup.cs file. Did I use the wrong template?</p>
  
  <p>I have also seen something similar to the following used:    </p>
  
  <pre><code>      scope = new CustomWebApplicationFactory&lt;Startup&gt;().Server.Host.Services.CreateScope();
  service = scope.ServiceProvider.GetRequiredService&lt;IDashboardService&gt;();
  context = scope.ServiceProvider.GetRequiredService&lt;DTBContext&gt;();
  </code></pre>
  
  <p>Which also seems to reference a startup class, so I really haven't been able to get this approach working either. Do I need to start again with a project that includes a Startup.cs folder? Or can I Unit Test the code above without it? I'm still researching this, but so far I cannot find a way to mock the dependencies of the service calls and the Unit of Work elements.</p>
  
  <p>Any suggestions or advice is greatly appreciated! Thank you!</p>
  
  <p>UPDATE: I think I now understand the xUnit.DependencyInjection, so I added a startup.cs class, and added the references referred to on their GitHub page. I also had to install Microsoft.Extensions.Hosting package to take care of a "does not have an implementation" error and now my project has a startup and the test that passed above now passes. I'm hoping that I can now handle dependencies within the testing project, but I'll find out.</p>