我有一个可移植类库,至少需要针对 .net 4.5 和 Silverlight 5。我在尝试在 VS 2012 中编写 MSTest 单元测试时遇到了问题,因为我的库不使用新的异步/等待范例。有什么方法可以测试这种方法吗?
public static void Get(string uri, string acceptHeader, Action<string> callback)
{
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Accept = acceptHeader;
request.BeginGetResponse(o =>
{
var r = o.AsyncState as HttpWebRequest;
try
{
var response = r.EndGetResponse(o);
using (var sr = new StreamReader(response.GetResponseStream()))
{
callback(sr.ReadToEnd());
}
}
catch (Exception ex)
{
throw new WebException(string.Format("Unable to access {0}", uri), ex);
}
}, request);
}