我想对下面使用 HttpClient 的 Search 类进行单元测试,
public class Search:ISearch{
HttpClient httpClient;
public Search(HttpClient httpClient){
this.httpClient = httpClient;
}
//use httClient to send request.
}
有没有办法模拟 HttpClient?我无法通过 Google 找到任何信息。
更新
发送可以模拟的 Http Web 请求是否有替代方法。我有以下代码:
public class Search:ISearch{
private static readonly string url = "http://www.google.com/search";
public Result SendSearch(string query){
string queryUrl = string.Format("{0}?q={1}", url, query);
var webRequest = WebRequest.Create(queryUrl);
///...
}