title: XUnit Test Web API Assert Status code
body: <p>I am new in <code>XUnit</code> tests for Web API application. I am trying to make assertion of my response value with the status code 404, but I am not sure how to do so.</p>
  
  <p>Here's my test code. </p>
  
  <blockquote>
    <p>Assume the input is null, the test is expected to return NotFound or 404
    status code.</p>
  </blockquote>
  
  <pre><code>  [Fact]
          public async Task getBooksById_NullInput_Notfound()
          {
  
              //Arrange
              var mockConfig = new Mock&lt;IConfiguration&gt;();
              var controller = new BookController(mockConfig.Object);
  
              //Act
              var response = await controller.GetBooksById(null);
  
              //Assert
              mockConfig.VerifyAll();
  
              Assert(response.StatusCode, HttpStatusCode.NotFound()); //How to achieve this?
  
          }
  </code></pre>
  
  <p>In the last line of this test method, I am not able to make <code>response.StatusCode</code> compile as in the response variable does not have <code>StatusCode</code> property. And there is no <code>HttpStatusCode</code> class that I can call... </p>
  
  <p>Here's my <code>GetBooksByID()</code> method in the controller class.</p>
  
  <pre><code> public class BookController : Controller
      {
          private RepositoryFactory _repositoryFactory = new RepositoryFactory();
  
          private IConfiguration _configuration;
  
          public BookController(IConfiguration configuration)
          {
              _configuration = configuration;
          }
  
  
  
          [HttpGet]
          [Route("api/v1/Books/{id}")]
          public async Task&lt;IActionResult&gt; GetBooksById(string id)
          {
              try
              {
                  if (string.IsNullOrEmpty(id))
                  {
                      return BadRequest();
                  }
  
                  var response = //bla
  
                  //do something
  
                  if (response == null)
                  {
                      return NotFound();
                  }
                  return new ObjectResult(response);
              }
              catch (Exception e)
              {
                  throw;
              }
          }
  </code></pre>
  
  <p>Thanks!</p>