测试RestSharp的反序列化没有适当的REST的API [英] Testing the Deserialization of RestSharp without proper REST-Api

查看:552
本文介绍了测试RestSharp的反序列化没有适当的REST的API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:这个问题的解决方案可以由约翰·希恩第一个注释中找到



我想用<一个HREF =htt​​p://restsharp.org/相对=nofollow> Restsharp 如REST的客户对我的项目。由于REST服务器尚未运行,我想测试客户端,而不服务器。我主要关注的是返回XML的响应的反序列化。 ?是否有可能使用RestSharp没有正确的RestSharp.RestResponse反序列化XML



我想它是这样的:

 公共无效testDeserialization()
{
XmlDeserializer D =新XmlDeserializer();
RestSharp.RestResponse响应=新RestSharp.RestResponse();
串XML = @<响应><项目1>有的文字< /项目1><项目2>另一种文字< /项目2><项目3>更文字< /项目3>< /响应>中;
response.Content = XML;

d.RootElement =响应;
响应R = d.Deserialize<应变及GT;(响应);
}

公共类响应
{
公共字符串项目1 {搞定;组; }
公共字符串项目2 {搞定;组; }
公共字符串项目3 {搞定;组; }
}



deserializations创建响应级,其中每场的对象空值。 ?有没有一种方法来测试是否(以及如何)的任何给定的XML将被RestSharp反序列化



编辑:为了更好的可读性 - 这是我使用的XML:

 <应变及GT; 
<项目1>有的文字< /项目1>
<项目2>另一种文字< /&项目2 GT;
<项目3>更文字< /项目3>
< /响应>


解决方案

我希望我这样做是正确的 - 但弄清这个问题就解决了​​,我复制的解决方案(由约翰·希恩评论):




您应该不必指定rootElement的。这只是用于当
根不是在顶层。尝试,让我知道,如果它的工作原理。
下面是我们如何测试项目的解串器:
https://github.com/restsharp/RestSharp/blob/master/RestSharp.Tests/XmlDeserializerTests.cs




(编辑:更新的链接到正确的文件)


EDIT: The solution to the question can be found in the first comment by John Sheehan!

i would like to use Restsharp as Rest-Client for my Project. Since the REST server is not running yet, I would like to test the client without the Server. My main focus is on the deserialization of the returning XML-Response. Is it possible to deserialize XML using RestSharp without a proper RestSharp.RestResponse?

I tried it like this:

public void testDeserialization()
{
    XmlDeserializer d = new XmlDeserializer();
    RestSharp.RestResponse response = new RestSharp.RestResponse();
    string XML = @"<Response><Item1>Some text</Item1><Item2>Another text</Item2><Item3>Even more text</Item3></Response>";
    response.Content = XML;

    d.RootElement = "Response";
    Response r = d.Deserialize<Response>(response);
}

public class Response
{
    public string Item1 { get; set; }
    public string Item2 { get; set; }
    public string Item3 { get; set; }
}

The deserializations creates an Object of the Response-Class, where every field is null. Is there a way to test if (and how) any given xml would be deserialized by RestSharp?

Edit: For better readability - this is the XML i'm using:

<Response>
    <Item1>Some text</Item1>
    <Item2>Another text</Item2>
    <Item3>Even more text</Item3>
</Response>

解决方案

I hope I'm doing this right - but to make clear this question is solved, i'm copying the solutions (from the comments by John Sheehan):

You shouldn't have to specify RootElement. That's only for when the root isn't at the top level. Try that and let me know if it works. Here's how we test the deserializer for the project: https://github.com/restsharp/RestSharp/blob/master/RestSharp.Tests/XmlDeserializerTests.cs

(EDIT: Updated link to correct file)

这篇关于测试RestSharp的反序列化没有适当的REST的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆