public async Task ShouldReturnJsonListOfSalaries() { // 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/salaries/list/*") .Respond("application/json", "{ 'salaries': [ {'id' : 98, 'timePeriod': '2019-Jun-15_2019-Jun-21', 'value': 300, 'employeeId': 1 }," + "{'id' : 99, 'timePeriod': '2019-Jun-22_2019-Jun-28', 'value': 320, 'employeeId': 1}" + " ] }"); // 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 salaries = await hqClient.ListSalariesForEmployee(1); // 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, salaries.Count); Assert.Equal(98, salaries[0].ID); Assert.Equal(99, salaries[1].ID); Assert.Equal("2019-Jun-15_2019-Jun-21", salaries[0].TimePeriod); Assert.Equal("2019-Jun-22_2019-Jun-28", salaries[1].TimePeriod); Assert.Equal(1, salaries[0].EmployeeID); Assert.Equal(1, salaries[1].EmployeeID); Assert.Equal(300f, salaries[0].Value); Assert.Equal(320f, salaries[1].Value); } }
public async Task ListSalariesForEmployee_ShouldNotThrowException() { // 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/salaries/list/*") .Respond(HttpStatusCode.NotFound); // Inject the handler or client into your application code var client = mockHttp.ToHttpClient(); using (var hqClient = new HQAPIClient(CommonHelpers.MockConfServ(), client)) { var salaries = await hqClient.ListSalariesForEmployee(1); Assert.Empty(salaries); } }
public async Task ListSalariesForEmployee_ShouldReturnEmptyListOfSalaries_WhenNoSalaries() { // 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/salaries/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 salaries = await hqClient.ListSalariesForEmployee(1); // 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(salaries); } }