我正在为 WCF Web 服务编写单元测试。我故意向抛出WebExceptionFault<string>
. 在单元测试中,被捕获的异常是属性中EndpointNotFoundException
的标准。我想验证响应的正文是否与我认为应该存在的字符串匹配。WebException
InnerException
但是,我遇到了一个问题,因为(它是 a )的Response
属性是 Disposed 并且无法读取。WebException
System.Net.SyncMemoryStream
我的代码:
Clubs actual;
try
{
//"201" is an invalid argument for the first parameter
actual = new Clubs(channel.GetClubs("201", "123"));
}
catch (EndpointNotFoundException e)
{
WebException w = e.InnerException as WebException;
Assert.IsNotNull(w);
HttpWebResponse resp = w.Response as HttpWebResponse;
Assert.IsNotNull(resp);
Assert.AreEqual(resp.StatusCode, HttpStatusCode.NotFound);
//Next line throws an ArgumentException saying Stream not readable
//Investigation shows it has been disposed
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
Assert.AreEqual(sr.ReadToEnd(), "League not allowed for selected club");
}
处置 InnerException 的属性是否正常?有没有办法解决?