使用webClient.UploadString进行C#单元测试

时间:2015-08-06 11:35:06

标签: c# unit-testing

我正在使用webClient.uploadString(uri,parameter)方法。 它命中一个返回字符串的url。每当有错误请求或其他类似错误时,服务器都会抛出异常。

在进行单元测试时,我需要将响应状态代码与某些特定的状态代码进行比较。

UploadString方法只返回字符串。

我的查询是: 如何从WebClient获取响应对象,以便我可以比较状态代码

1 个答案:

答案 0 :(得分:1)

如果请求失败,您只能确定状态代码:

WebClient

如果您想在请求成功时确定状态代码,则需要monkey patch WebClient ...

我实际上建议您使用var client = new HttpClient(); HttpResponseMessage response = client.GetAsync("...").Result; HttpStatusCode status = response.StatusCode; string result = response.Content.ReadAsStringAsync().Result; 代替 with recursive valid_nodes as ( -- this is the inner "CONNECT BY" query from your example select id from hierarchy where node_name = 'some-pattern' union all select c.id from hierarchy c join valid_nodes p on c.id = p.pig_ear_id ), final_tree as ( -- this is outer query from your example select node_name as node, 1 as level from hierarchy where NVL (pig_ear_id, 0) = 0 union all select c.node_name, p.level + 1 from hierarchy c join final_tree p on p.id = c.pig_ear_id where id in (select id from valid_nodes) -- and here we re-use the previous CTE ) select rpad(node, level - 1)||node, level from final_tree; 。它有一个很多更好的界面:

recursive

请注意,您所描述的内容不是单元测试,而是HttpClient,并且似乎无用。