IHttpActionResult匿名对象 - 验证结果(IHttpActionResult Anonymous Object - Validate Results)

我正在尝试为我们的一些webapi调用创建测试,并且我无法访问结果。 在我看过的所有例子中,他们都使用了OkNegotiatedContentResult。 问题在于,在我们的web api调用中,我们经常会将数据包装在匿名对象中,所以我们可以组合数据集。 我可能忽略了一些明显的东西,但我似乎无法弄清楚检查结果信息以验证它的正确方法。

WebApi片段

var orderInfo = new { Customer = customerInfo, Order = orderInfo } return Ok(orderInfo);

Api测试代码片段

[TestMethod] public void TestGetOrderInfo() { var controller = new OrderController(_repo); IHttpActionResult results = controller.GetOrderInfo(46); Assert.IsNotNull(results); }

如何在涉及匿名类型时使用OkNegotiatedContentResult检查结果?

I am trying to create a test for some of our webapi calls and I am having difficulties accessing the results. In all of the examples I have viewed they were using OkNegotiatedContentResult. The problem is that in our web api calls we are often times wrapping the data in anonymous objects so we can combine data sets. I am probably overlooking something obvious, but I can't seem to figure out the proper way to inspect the result information to validate it.

WebApi Snippet

var orderInfo = new { Customer = customerInfo, Order = orderInfo } return Ok(orderInfo);

Api Test Snippet

[TestMethod] public void TestGetOrderInfo() { var controller = new OrderController(_repo); IHttpActionResult results = controller.GetOrderInfo(46); Assert.IsNotNull(results); }

How can I inspect the results using the OkNegotiatedContentResult when an anonymous type is involved?

最满意答案

匿名类型问题的原因是它们是内部类型而不是公共的,所以你的测试不能使用它们。

如果您将一个InternalsVisibleTo属性添加到您的webapi项目中,您将能够通过动态引用结果及其内容,例如:

[TestMethod] public void TestGetOrderInfo() { var controller = new OrderController(_repo); dynamic results = controller.GetOrderInfo(46); dynamic content = results.Content; ... }

The reason for the problems with anonymous types is that they are internal types rather than public, so your tests can't use them.

If you add an InternalsVisibleTo attribute to your webapi project you'll then be able to reference the result and its Content via dynamic eg:

[TestMethod] public void TestGetOrderInfo() { var controller = new OrderController(_repo); dynamic results = controller.GetOrderInfo(46); dynamic content = results.Content; ... }IHttpActionResult匿名对象 - 验证结果(IHttpActionResult Anonymous Object - Validate Results)

我正在尝试为我们的一些webapi调用创建测试,并且我无法访问结果。 在我看过的所有例子中,他们都使用了OkNegotiatedContentResult。 问题在于,在我们的web api调用中,我们经常会将数据包装在匿名对象中,所以我们可以组合数据集。 我可能忽略了一些明显的东西,但我似乎无法弄清楚检查结果信息以验证它的正确方法。

WebApi片段

var orderInfo = new { Customer = customerInfo, Order = orderInfo } return Ok(orderInfo);

Api测试代码片段

[TestMethod] public void TestGetOrderInfo() { var controller = new OrderController(_repo); IHttpActionResult results = controller.GetOrderInfo(46); Assert.IsNotNull(results); }

如何在涉及匿名类型时使用OkNegotiatedContentResult检查结果?

I am trying to create a test for some of our webapi calls and I am having difficulties accessing the results. In all of the examples I have viewed they were using OkNegotiatedContentResult. The problem is that in our web api calls we are often times wrapping the data in anonymous objects so we can combine data sets. I am probably overlooking something obvious, but I can't seem to figure out the proper way to inspect the result information to validate it.

WebApi Snippet

var orderInfo = new { Customer = customerInfo, Order = orderInfo } return Ok(orderInfo);

Api Test Snippet

[TestMethod] public void TestGetOrderInfo() { var controller = new OrderController(_repo); IHttpActionResult results = controller.GetOrderInfo(46); Assert.IsNotNull(results); }

How can I inspect the results using the OkNegotiatedContentResult when an anonymous type is involved?

最满意答案

匿名类型问题的原因是它们是内部类型而不是公共的,所以你的测试不能使用它们。

如果您将一个InternalsVisibleTo属性添加到您的webapi项目中,您将能够通过动态引用结果及其内容,例如:

[TestMethod] public void TestGetOrderInfo() { var controller = new OrderController(_repo); dynamic results = controller.GetOrderInfo(46); dynamic content = results.Content; ... }

The reason for the problems with anonymous types is that they are internal types rather than public, so your tests can't use them.

If you add an InternalsVisibleTo attribute to your webapi project you'll then be able to reference the result and its Content via dynamic eg:

[TestMethod] public void TestGetOrderInfo() { var controller = new OrderController(_repo); dynamic results = controller.GetOrderInfo(46); dynamic content = results.Content; ... }