title: Writing a test for a method within an asp.net core web api
body: <p>I will like to write a xunit test for the controller method below</p>
  
  <pre><code>        [HttpGet]
          [Route("GetPosts")]
          public async Task&lt;IActionResult&gt; GetPosts()
          {
              try
              {
                  var posts = await postRepository.GetPosts();
                  if (posts == null)
                  {
                      return NotFound();
                  }
  
                  return Ok(posts);
              }
              catch (Exception)
              {
                  return BadRequest();
              }
          }
  </code></pre>
  
  <p>My viewmodel looks like this. </p>
  
  <pre><code>        public class PostViewModel
              {
                  public int PostId { get; set; }
                  public string Title { get; set; }
                  public string Description { get; set; }
                  public int? CategoryId { get; set; }
                  public DateTime? CreatedDate { get; set; }
                  public string CategoryName { get; set; }
              }
  </code></pre>
  
  <p>Here is what my repository looks like. </p>
  
  <pre><code>        public async Task&lt;List&lt;PostViewModel&gt;&gt; GetPosts()
                  {
                      if (db != null)
                      {
                          return await (from p in db.Post
                                        from c in db.Category
                                        where p.CategoryId == c.Id
                                        select new PostViewModel
                                        {
                                            PostId = p.PostId,
                                            Title = p.Title,
                                            Description = p.Description,
                                            CategoryId = p.CategoryId,
                                            CategoryName = c.Name,
                                            CreatedDate = p.CreatedDate
                                        }).ToListAsync();
                      }
  
                      return null;
          }
  </code></pre>
  
  <p>I started getting this error message</p>
  
  <pre><code>        cannot convert from 'System.Collections.Generic.List&lt;CoreServices.ViewModel.PostViewModel&gt;' 
          to 'System.Threading.Tasks.Task&lt;System.Collections.Generic.List&lt;CoreServices.ViewModel.PostViewModel&gt;&gt;' 
  </code></pre>
  
  <p>Here is what my xunit test looks like.</p>
  
  <pre><code>        public class PostControllerTest
          {
              private readonly Mock&lt;IPostRepository&gt; _mockRepo;
              private readonly PostController  _controller;
  
              public PostControllerTest()
              {
                  _mockRepo = new Mock&lt;IPostRepository&gt;();
                  _controller = new PostController(_mockRepo.Object);
              }
  
              [Fact]
              public void GetPosts_TaskExecutes_ReturnsExactNumberOfPosts()
              {
                  _mockRepo.Setup(repo =&gt; repo.GetPosts())
                      .Returns(new List&lt;PostViewModel&gt;() { new PostViewModel(), new PostViewModel() });
  
                      //var result = 
                      //Assert.True()
              }
          }
  </code></pre>
  
  <p>I will like to complete my first test which will show the count of the post is 2 (mocking a dependency). 
  How can I write this/complete this test ?</p>