You cannot mock this with a mocking structure like Rhino-Mocks, because for this you need either interface or virtual methods in the HttpClient class. The mocking structure will create a layout for you that either implements the methods defined on the interface or overrides the methods of your virtual class.
So, either you end the HttpClient class, and let it implement the interface, or donβt mock it.
If you change the code to something like this:
public class Search:ISearch { private static readonly string url = "http://www.google.com/search"; private readonly IWebRequestCreator _generator; public Search(IWebRequestCreator generator) { _generator = generator; } public Result SendSearch(string query) { var queryUrl = string.Format("{0}?q={1}", url, query); var webRequest = _generator.Create(queryUrl);
If you create a class that implements IWebRequestCreator , then you can mock functionality. The implementation class will simply call WebRequest.Create(queryUrl);
source share