Using Extension Methods to clean up Mocks (MoQ)
Mock’s can quickly become an ugly beast. They require lots of complex setup and verification. I have had good luck with using extension methods to simplify the complexity. Assume we have the following code to implement our data layer on top of some ReST API. public interface IWebClient { string DownloadString(string url); string UploadValues(string method, string address, NameValueCollection data); } public class WebClientWrapper : IWebClient { private WebClient client; public WebClientWrapper() { client = new WebClient(); } public string DownloadString(string url) { return client.DownloadString(url); } public string UploadValues(string method, string address, NameValueCollection data) { return Encoding.UTF8.GetString(client.UploadValues(address, method, data)); } } In the business/domain layer, we have a class that uses IWebClient to get some information from the API. »