From what I understand, you do not want to handle exceptions thrown by the code, but rather create them in your action methods. In the case where exceptions are excluded from other areas of the code (other actions that you call inside your actions or other filters, you can use exception filters or global error handling).
Therefore, I would go with your second approach (although you do not need to tweak the results of actions much). Your code is actually much simpler than unit test with IHttpActionResult, because you can directly check the type of result. In fact, one of the reasons IHttpActionResults adds simplification to unit testing.
The flow of your code is simpler because you do not need to throw to generate errors, and you can always check the contents of ok (returnValue), as you can see below.
[TestMethod] public void GetProduct_ShouldReturnCorrectProduct() { var testProducts = GetTestProducts(); var controller = new SimpleProductController(testProducts); var result = controller.GetProduct(4) as OkNegotiatedContentResult<Product>; Assert.IsNotNull(result); Assert.AreEqual(testProducts[3].Name, result.Content.Name); }
http://www.asp.net/web-api/overview/testing-and-debugging/unit-testing-with-aspnet-web-api
source share