title: xUnit.net IsType Equivalent in MS Test That Returns Type
body: <p>I'm following <a href="https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/testing?view=aspnetcore-1.1#comments-container" rel="nofollow noreferrer">this guide</a> and I'm trying to write a similar test with MS Test.  Does MS Test have an <a href="https://github.com/xunit/assert.xunit/blob/master/TypeAsserts.cs#L73" rel="nofollow noreferrer"><code>IsType()</code></a> that <strong>returns the object</strong> when the cast is successful?</p>
  
  <p>From <a href="https://xunit.github.io/docs/comparisons" rel="nofollow noreferrer">this comparison</a>, I see I can use <code>IsInstanceOfType()</code> but the return type is <code>void</code>.</p>
  
  <hr>
  
  <p>I'm trying to implement this line in MS Test: 
  <code>var badRequestResult = Assert.IsType&lt;BadRequestObjectResult&gt;(result);</code></p>
  
  <pre class="lang-cs prettyprint-override"><code>[Fact]
  public async Task IndexPost_ReturnsBadRequestResult_WhenModelStateIsInvalid()
  {
      // Arrange
      var mockRepo = new Mock&lt;IBrainstormSessionRepository&gt;();
      mockRepo.Setup(repo =&gt; repo.ListAsync()).Returns(Task.FromResult(GetTestSessions()));
      var controller = new HomeController(mockRepo.Object);
      controller.ModelState.AddModelError("SessionName", "Required");
      var newSession = new HomeController.NewSessionModel();
  
      // Act
      var result = await controller.Index(newSession);
  
      // Assert
      var badRequestResult = Assert.IsType&lt;BadRequestObjectResult&gt;(result);
      Assert.IsType&lt;SerializableError&gt;(badRequestResult.Value);
  }
  </code></pre>
  
  <hr>
  
  <p>Yes, I could cast using <code>as</code> and then use <code>IsInstanceOfType</code>...  just wondering if there is a one-liner.</p>
  
  <pre class="lang-cs prettyprint-override"><code>var badRequestResult = result as BadRequestObjectResult;
  Assert.IsInstanceOfType(badRequestResult, typeof(BadRequestObjectResult));
  </code></pre>
  
  <hr>
  
  <p>Reference: <a href="https://github.com/Microsoft/testfx/issues/413" rel="nofollow noreferrer">Add to MSTest Request</a></p>