MockHttp is a testing layer for Microsoft's HttpClient library. It allows stubbed responses to be configured for matched HTTP requests and can be used to test your application's service layer.
var mockHttp = new MockHttpMessageHandler(); // Setup a respond for the user api (including a wildcard in the URL) mockHttp.When("http://localhost/api/user/*") .Respond("application/json", "{'name' : 'Test McGee'}"); // Respond with JSON // Inject the handler or client into your application code var client = mockHttp.ToHttpClient(); var response = await client.GetAsync("http://localhost/api/user/1234"); // or without async: var response = client.GetAsync("http://localhost/api/user/1234").Result; var json = await response.Content.ReadAsStringAsync(); // No network connection required Console.Write(json); // {'name' : 'Test McGee'}
Part 1: Trials and Tribulations of TDD
Part 2: Naming Tests; Mocking Frameworks; Dependency Injection
Part 3: Mocks vs. Stubs; Test Frameworks; Assertions; ReSharper Accelerators
Part 4: Tests as Documentation; False Positive Results; Component Isolation
Part 5: Tests vs. Code; Refactor Friendliness; Test Parameterization
Part 6: Mini-Factory Pattern; Don’t Care Terms