Testing the Deserialization of RestSharp without proper REST-Api

一笑奈何 提交于 2019-12-04 01:34:55

问题


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>

回答1:


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)



来源:https://stackoverflow.com/questions/8896911/testing-the-deserialization-of-restsharp-without-proper-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!