[英]C# Wcf Web API Deserializing Response
I am trying to unit test a web service that returns a non primitive.我正在尝试对返回非原语的 web 服务进行单元测试。 The response can be either xml or json depending on the request.
根据请求,响应可以是 xml 或 json。 In my test it'd be great if I could deserialize the content body to one of my objects.
在我的测试中,如果我可以将内容主体反序列化为我的对象之一,那就太好了。 Here's some code:
这是一些代码:
[WebGet(UriTemplate = "{arg1}/{arg2}")]
public HttpResponseMessage<MyType> GetSomethingCool(string arg1, long arg2)
{
return new HttpResonseMessage<MyTpe>(new MyType());
}
public class MyType
{
public string Property1 { get; set; }
public long Property2 { get; set; }
public string Property3 { get; set; }
}
And my test:我的测试:
[Test]
public void MyTestForTheWebService_ReturnsText()
{
Service1 service = new Service1 (_mockRepository.Object);
HttpResponseMessage<MyType> result = service.GetSomethingCool("Something", **);
Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
MyType resultContent = result.Content.ReadAs<MyType>(); // doesn't work
Assert.AreEqual("expected value", resultContent .Property1);
.....
So basically I need to be able to turn result.Content to a MyType and don't know how.所以基本上我需要能够将 result.Content 转换为 MyType 并且不知道如何。 Thanks
谢谢
Try doing:尝试做:
MyType resultContent = (MyType)result.Content.ReadAs();
I believe you are running into a known bug.我相信你遇到了一个已知的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.