public async Task ShouldReturnJsonListOfEmployees() { // https://github.com/richardszalay/mockhttp var mockHttp = new MockHttpMessageHandler(); // Setup a respond for the user api (including a wildcard in the URL) var mockedRequest = mockHttp.When(CommonHelpers.baseUrl + "/api/employees/list/*") .Respond("application/json", "{ 'employees': [ {'name' : 'Jan Kow', 'id': 1, 'email': '*****@*****.**', 'isManager': false }," + " {'name' : 'Teresa Kow', 'id': 123, 'email': '*****@*****.**', 'isManager': true } ] }"); // Respond with JSON // Inject the handler or client into your application code var client = mockHttp.ToHttpClient(); using (var hqClient = new HQAPIClient(CommonHelpers.MockConfServ(), client)) { var employees = await hqClient.ListEmployees(100); // GetMatchCount will return the number of times a mocked request (returned by When / Expect) was called // https://github.com/richardszalay/mockhttp#verifying-matches Assert.Equal(1, mockHttp.GetMatchCount(mockedRequest)); Assert.Equal(2, employees.Count); Assert.Equal(1, employees[0].ID); Assert.Equal(123, employees[1].ID); Assert.Equal(false, employees[0].IsManager); Assert.Equal(true, employees[1].IsManager); } }
public async Task ListEmployees_ShouldReturnEmptyListOfEmployees_WhenNoEmployees() { // https://github.com/richardszalay/mockhttp var mockHttp = new MockHttpMessageHandler(); // Setup a respond for the user api (including a wildcard in the URL) var mockedRequest = mockHttp.When(CommonHelpers.baseUrl + "/api/employees/list/*") .Respond("application/json", "[]"); // Respond with JSON // Inject the handler or client into your application code var client = mockHttp.ToHttpClient(); using (var hqClient = new HQAPIClient(CommonHelpers.MockConfServ(), client)) { var employees = await hqClient.ListEmployees(100); Assert.NotNull(employees); // GetMatchCount will return the number of times a mocked request (returned by When / Expect) was called // https://github.com/richardszalay/mockhttp#verifying-matches Assert.Equal(1, mockHttp.GetMatchCount(mockedRequest)); Assert.Empty(employees); } }