From d6f1880f47b84e2a702e19c1f41d97aa5ef5ef9a Mon Sep 17 00:00:00 2001 From: Damiano Patane Date: Mon, 28 Oct 2019 16:06:15 +0100 Subject: [PATCH] Refactoring some unit tests, creating first steps to test retry counts. Please give feedback on recursive method execution testing. --- tests/Xablu.WebApiClient.Tests/RefitTests.cs | 37 +++++++++++++++++++ .../WebApiClientTests.cs | 20 ++++++---- 2 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 tests/Xablu.WebApiClient.Tests/RefitTests.cs diff --git a/tests/Xablu.WebApiClient.Tests/RefitTests.cs b/tests/Xablu.WebApiClient.Tests/RefitTests.cs new file mode 100644 index 0000000..ea5ae10 --- /dev/null +++ b/tests/Xablu.WebApiClient.Tests/RefitTests.cs @@ -0,0 +1,37 @@ +using System; +using System.Threading.Tasks; +using Moq; +using Xablu.WebApiClient.Client; +using Xablu.WebApiClient.Enums; +using Xablu.WebApiClient.Services.GraphQL; +using Xunit; + +namespace Xablu.WebApiClient.Tests +{ + public class RefitTests + { + [Fact] + public void CheckRetryCount() + { + // mock dependencies + var refitService = new Mock>(); + var graphqlService = new Mock(); + var mockedAPI = new Mock(); + + // setup dependency mocks + refitService.SetupGet(s => s.GetByPriority(Priority.UserInitiated)).Returns(mockedAPI.Object); + mockedAPI.Setup(api => api.Get()).Returns(Task.CompletedTask); + + var client = new WebApiClient(refitService.Object, graphqlService.Object); + + // complete call + var call = client.Call(api => api.Get()); + + // check if call has retried according to count + if (call.Status != TaskStatus.Faulted) + { + // do something next to check retry counts... + } + } + } +} diff --git a/tests/Xablu.WebApiClient.Tests/WebApiClientTests.cs b/tests/Xablu.WebApiClient.Tests/WebApiClientTests.cs index 16dbeca..c8e46d2 100644 --- a/tests/Xablu.WebApiClient.Tests/WebApiClientTests.cs +++ b/tests/Xablu.WebApiClient.Tests/WebApiClientTests.cs @@ -3,6 +3,8 @@ using Moq; using Refit; using Xablu.WebApiClient.Client; +using Xablu.WebApiClient.Enums; +using Xablu.WebApiClient.Services.GraphQL; using Xunit; namespace Xablu.WebApiClient.Tests @@ -19,34 +21,36 @@ public class WebApiClientTests public void EnsureUserInitiatedIsSelectedByDefault() { // mock dependencies - var service = new Mock>(); + var refitService = new Mock>(); + var graphqlService = new Mock(); var mockedAPI = new Mock(); // setup dependency mocks - service.SetupGet(s => s.UserInitiated).Returns(mockedAPI.Object); + refitService.SetupGet(s => s.GetByPriority(Priority.UserInitiated)).Returns(mockedAPI.Object); mockedAPI.Setup(api => api.Get()).Returns(Task.CompletedTask); - var client = new WebApiClient(service.Object); + var client = new WebApiClient(refitService.Object, graphqlService.Object); // execute sentence to test client.Call(api => api.Get()); // make sure UserInitiated was called - service.VerifyGet(s => s.UserInitiated); + refitService.VerifyGet(s => s.GetByPriority(Priority.UserInitiated)); } [Fact] public void EnsureOperationIsCalledInTheRefitAPI() { // mock dependencies - var service = new Mock>(); + var refitService = new Mock>(); + var graphqlService = new Mock(); var mockedAPI = new Mock(); // setup dependency mocks - service.SetupGet(s => s.UserInitiated).Returns(mockedAPI.Object); - mockedAPI.Setup(api => api.Get()).Returns(Task.CompletedTask).Verifiable(); + refitService.SetupGet(s => s.GetByPriority(Priority.UserInitiated)).Returns(mockedAPI.Object); + mockedAPI.Setup(api => api.Get()).Returns(Task.CompletedTask); - var client = new WebApiClient(service.Object); + var client = new WebApiClient(refitService.Object, graphqlService.Object); // execute sentence to test client.Call(api => api.Get());