C# Wcf Web API 反序列化响应
我正在尝试对返回非基元的 Web 服务进行单元测试。根据请求,响应可以是 xml 或 json。在我的测试中,如果我可以将内容主体反序列化为我的对象之一,那就太好了。这是一些代码:
[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; }
}
我的测试:
[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);
.....
所以基本上我需要能够将 result.Content 转换为 MyType 并且不知道如何。谢谢
I am trying to unit test a web service that returns a non primitive. The response can be either xml or json depending on the request. 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. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这样做:
我相信您遇到了一个已知的错误。
Try doing:
I believe you are running into a known bug.