我有一个便携式类库,需要至少定位.net 4.5和Silverlight 5.我遇到了一个试图在VS 2012中编写MSTest单元测试的问题,因为我的库没有使用新的async / await范例。有什么方法可以测试这种方法吗?
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);
}
答案 0 :(得分:2)
无法抗拒重构代码。您可以使用闭包执行以下操作
public static void Get(string uri, string acceptHeader, Action<string> callback)
{
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Accept = acceptHeader;
request.BeginGetResponse(o =>
{
try
{
var response = request.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);
}
}, null);
}
然而,在一天结束时,您可以执行以下操作
public async static void Get(string uri, string acceptHeader, Action<string> callback)
{
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Accept = acceptHeader;
var response = await Task.Factory.FromAsync(
request.BeginGetRequestStream ,
request.EndGetRequestStream ,
uri,
null);
using (var sr = new StreamReader(response))
{
callback(sr.ReadToEnd());
}
}
好的,我就是这样做的
void Main()
{
{...}
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Accept = acceptHeader;
var response = await request.DownloadStringTaskAwait();
DoSomeStuff(response);
}
// Define other methods and classes here
public static class HttpWebRequestExtension
{
public async Task<string> DownloadStringTaskAwait(this HttpWebRequest request)
{
var response = await Task.Factory.FromAsync<Stream>(
request.BeginGetRequestStream,
request.EndGetRequestStream,
null);
using (var sr = new StreamReader(response))
{
return sr.ReadToEnd();
}
}
}
答案 1 :(得分:2)
首先,我建议您重新考虑async
/ await
。这是未来的潮流。 Microsoft.Bcl.Async为面向.NET 4.5和SL5的可移植库提供async
支持。
但如果您不想这样做,您仍然可以使用async
单元测试:
[TestMethod]
public async Task Get_RetrievesExpectedString()
{
var tcs = new TaskCompletionSource<string>();
var client = new ... // arrange
client.Get(uri, acceptHeader, result =>
{
tcs.SetResult(result);
});
var actual = await tcs.Task;
Assert.AreEqual(expected, actual);
}
或者,如果你愿意,你可以做“老派”:
[TestMethod]
public void Get_RetrievesExpectedString()
{
var mre = new ManualResetEvent(initialState: false);
string actual = null;
var client = new ... // arrange
client.Get(uri, acceptHeader, result =>
{
actual = result;
mre.Set();
});
mre.WaitOne();
Assert.AreEqual(expected, actual);
}