title: Unit testing ApiController methods with status codes not in HttpStatusCode enum
body: <p>I have an ApiController method that looks like this...</p>
  
  <pre><code>    [HttpPost]
      public IHttpActionResult Post([FromBody] GloballyEnumeratedMessage gem)
      {
          var result = _gemPublisher.PublishGem(gem);
  
          if (result.Success)
          {
              return Ok();
          }
  
          return new StatusCodeResult((HttpStatusCode)422, this);
      }
  </code></pre>
  
  <p>My test for for checking the Ok result is as follows.</p>
  
  <pre><code>    [TestMethod]
      public void Post_ValidGem_Returns200()
      {
          _gemPublisherMock.Setup(v =&gt; v.PublishGem(It.IsAny&lt;GloballyEnumeratedMessage&gt;())).Returns(ResultStatus.FromSuccess());
  
          var controller = new PublishGemController(_gemPublisherMock.Object);
  
          // Act
          IHttpActionResult actionResult = controller.Post(It.IsAny&lt;GloballyEnumeratedMessage&gt;());
  
          // Assert
          Assert.IsInstanceOfType(actionResult, typeof(OkResult));
      }
  </code></pre>
  
  <p>Before I was returning a 422, I was returning a BadRequest and the test looked like this:</p>
  
  <pre><code>    [TestMethod]
      public void Post_InvalidGem_Returns400()
      {
          _gemPublisherMock.Setup(v =&gt; v.PublishGem(It.IsAny&lt;GloballyEnumeratedMessage&gt;())).Returns(ResultStatus.FromFailure());
  
          var controller = new PublishGemController(_gemPublisherMock.Object);
  
          // Act
          IHttpActionResult actionResult = controller.Post(It.IsAny&lt;GloballyEnumeratedMessage&gt;());
  
          // Assert
          Assert.IsInstanceOfType(actionResult, typeof(BadRequestResult));
      }
  </code></pre>
  
  <p>I wanted to reserve BadRequest for another potential failure so decided to change the Post method to return a 422 if the Publish fails. What would my test to check for the 422 status code look like?</p>
  
  <p>EDIT: Solved based off a comment to the question </p>
  
  <pre><code>[TestMethod]
  public void Post_InvalidGem_Returns422()
  {
      _gemPublisherMock.Setup(v =&gt; v.PublishGem(It.IsAny&lt;GloballyEnumeratedMessage&gt;())).Returns(ResultStatus.FromFailure());
  
      var controller = new PublishGemController(_gemPublisherMock.Object);
  
      // Act
      IHttpActionResult actionResult = controller.Post(It.IsAny&lt;GloballyEnumeratedMessage&gt;());
      var result = (StatusCodeResult) actionResult;
  
      // Assert
      Assert.IsInstanceOfType(actionResult, typeof(StatusCodeResult));
      Assert.AreEqual(422, (int)result.StatusCode);
  }
  </code></pre>