title: Mock Async method on Service using Moq
body: <p>I am working in a .Net Core API. I wish to unit test the <code>GetArtists</code> method on the <code>ArtistsController</code>.</p>
  
  <p><strong>CODE</strong></p>
  
  <p>Here is my controller code:</p>
  
  <pre><code>[Route("artists")]
  public class ArtistsController : Controller
  {
    private readonly IPermissionsService _permissionsService;
    private readonly IArtistsService _artistsService;
    private readonly ILogger&lt;ArtistsController&gt; _logger;
  
    public ArtistsController(IPermissionsService permissionsService, IArtistsService artistsService, ILogger&lt;ArtistsController&gt; logger)
    {
      _permissionsService = permissionsService ?? throw new ArgumentNullException(nameof(permissionsService));
      _artistsService = artistsService ?? throw new ArgumentNullException(nameof(artistsService));
      _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }
  
    [HttpGet]
    public async Task&lt;IActionResult&gt; GetArtists()
    {
      var permissions = await _permissionsService.GetPermissionsAsync(HttpContext);
      var artists = _artistsService.GetAllArtists(permissions.UserId, permissions.IsAdministrator);
      return Ok( new { artists });
    }
  }
  </code></pre>
  
  <p>And here is the test method I am writing:</p>
  
  <pre><code>[TestClass]
  public class ArtistsControllerTests
  {
    private readonly Mock&lt;IPermissionsService&gt; _mockPermissionsService = new Mock&lt;IPermissionsService&gt;();
    private readonly Mock&lt;IArtistsService&gt; _mockArtistsService = new Mock&lt;IArtistsService&gt;();
    private readonly Mock&lt;ILogger&lt;ArtistsController&gt;&gt; _mockLogger = new Mock&lt;ILogger&lt;ArtistsController&gt;&gt;();
  
    public void Setup()
    {
      _mockArtistsService.Reset();
      _mockPermissionsService
        .Setup(service =&gt; service.GetPermissionsAsync(It.IsAny&lt;HttpContext&gt;()))
        .Returns(Task.FromResult(new Permissions { UserId = "112233", IsAdministrator = false }));
      _mockArtistsService.Setup(service =&gt; service.GetAllArtists(It.IsAny&lt;string&gt;(), false)).Returns(new ArtistCardDtoCollection());
    }
  
    [TestMethod]
    public async Task GetArtists_ReturnsOKStatusCode()
    {
      // arrange
      var artistsController = new ArtistsController(_mockPermissionsService.Object, _mockArtistsService.Object, _mockLogger.Object);
      // act
      var getArtistsResult = await artistsController.GetArtists();
      var okResult = getArtistsResult as OkObjectResult;
      // assert
      Assert.IsInstanceOfType(okResult, typeof(OkObjectResult));
    }
  }
  </code></pre>
  
  <p>Here is the <code>IPermissionsService</code> and the <code>Permissions</code> class.</p>
  
  <pre><code>public interface IPermissionsService
  {
    Task&lt;Permissions&gt; GetPermissionsAsync(HttpContext httpContext);
  }
  
  public class Permissions
  {
    public string UserId { get; set; }
    public bool IsAdministrator { get; set; }
  }
  </code></pre>
  
  <p>When I run that, I get the following error:</p>
  
  <blockquote>
    <p>Project.ArtistsControllerTests.GetArtists_ReturnsOKStatusCode threw exception: 
    System.NullReferenceException: Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p>When debugging, I found out that <code>var permissions = await _permissionsService.GetPermissionsAsync(HttpContext);</code> returns <code>null</code>.</p>
  
  <p>I must have an issue with the way I am mocking that:</p>
  
  <pre><code>_mockPermissionsService
      .Setup(service =&gt; service.GetPermissionsAsync(It.IsAny&lt;HttpContext&gt;()))
  </code></pre>
  
  <p>Why wouldn't the above work?</p>