title: Unit testing ASP.NET Web API 2 Controller which returns custom result
body: <p>I have a Web API 2 controller which has an action method like this:</p>
  
  <pre><code>public async Task&lt;IHttpActionResult&gt; Foo(int id)
  {
      var foo = await _repository.GetFooAsync(id);
      return foo == null ? (IHttpActionResult)NotFound() : new CssResult(foo.Css);
  }
  </code></pre>
  
  <p>Where <code>CssResult</code> is defined as:</p>
  
  <pre><code>public class CssResult : IHttpActionResult
  {
      private readonly string _content;
  
      public CssResult(string content)
      {
          content.ShouldNotBe(null);
          _content = content;
      }
  
      public Task&lt;HttpResponseMessage&gt; ExecuteAsync(CancellationToken cancellationToken)
      {
          var response = new HttpResponseMessage(HttpStatusCode.OK)
          {
              Content = new StringContent(_content, Encoding.UTF8, "text/css")
          };
  
          return Task.FromResult(response);
      }
  }
  </code></pre>
  
  <p>How do i write a unit test for this?</p>
  
  <p>I tried this:</p>
  
  <pre><code>var response = await controller.Foo(id) as CssResult;
  </code></pre>
  
  <p>But i don't have access to the actual content, e.g i want to verify that the actual content of the response is the CSS i expect.</p>
  
  <p>Any help please?</p>
  
  <p>Is the solution to simply make the <code>_content</code> field public? (that feels dirty)</p>