c# - Flurl: cannot serialize JSON string received from HttpTest -


i want receive json rest api , translate pocos. it's supposed simple turns out not :(

in unit test have string of sample json data api sends:

string mockjsonresponse = @"[{                 ""project_name"": ""mailjet support"",                 ""cluster_name"": ""24/7 support"",                 ""is_billable"": ""1"",                 ""usedtime"": ""128""             },             {                 ""project_name"": ""caring"",                 ""cluster_name"": ""caring"",                 ""is_billable"": ""0"",                 ""usedtime"": ""320""             },             {                 ""project_name"": ""engagement"",                 ""cluster_name"": ""community"",                 ""is_billable"": ""0"",                 ""usedtime"": ""8""             }]"; 

which send code test through httptest:

httptest.respondwithjson(mockjsonresponse); 

i trying receive in code:

dynamic response = "http://api.com".getjsonlistasync(); 

but fails generic error in test explorer:

result message: flurl.http.flurlhttpexception : request http://api.com failed.

further digging seems cannot serialize string poco. i've tried doing manual serializing directly string variable above , translates model class easily, not have been code structure problem.

// same string variable above var jsons = jsonconvert.deserializeobject<list<model>>(mockjsonresponse); // runs fine 

all of these failed:

dynamic response = await "http://www.api.com".getjsonasync(); dynamic response = await "http://www.api.com".getjsonasync<model>(); var response = await "http://www.api.com".getjsonasync<model>();     ilist<dynamic> response = await "http://www.api.com".getjsonlistasync(); 

the model class:

public class model {     public string project_name { get; set; }     public string cluster_name { get; set; }     public string is_billable { get; set; }     public string usedtime { get; set; } } 

edit tried getting string getstringasync, , seems string got mangled somehow. string passed on jsonconvert.deserialize<model>() fail test. visual studio debugger shows. there's lot of escape characters.

screenshot of string received httptest in visual studio debugger

respondwithjson takes object serialized json you, not string serialized. represent test response anonymous object , should good:

var mockjsonresponse = new[] {     new {         project_name = "mailjet support",         cluster_name = "24/7 support",         is_billable = "1",         usedtime = "128"     },                    new {                         project_name = "caring",         cluster_name = "caring",         is_billable = "0",         usedtime = "320"     },                  new {                        project_name = "engagement",         cluster_name = "community",         is_billable = "0",         usedtime = "8"     } };  httptest.respondwithjson(mockjsonresponse); 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

unity3d - Fatal error- Monodevelop-Unity failed to start -