diff --git a/Deepgram.Tests/ClientTests/DeepgramClientTest.cs b/Deepgram.Tests/ClientTests/DeepgramClientTest.cs index 50bf6711..dbde0ac1 100644 --- a/Deepgram.Tests/ClientTests/DeepgramClientTest.cs +++ b/Deepgram.Tests/ClientTests/DeepgramClientTest.cs @@ -1,10 +1,4 @@ -using System; -using Deepgram.Clients; -using Deepgram.Interfaces; -using Deepgram.Tests.Fakers; -using Deepgram.Utilities; -using Xunit; - + namespace Deepgram.Tests.ClientTests { public class DeepClientTests diff --git a/Deepgram.Tests/ClientTests/KeyClientTests.cs b/Deepgram.Tests/ClientTests/KeyClientTests.cs index e351ea7c..ae54dbaf 100644 --- a/Deepgram.Tests/ClientTests/KeyClientTests.cs +++ b/Deepgram.Tests/ClientTests/KeyClientTests.cs @@ -1,10 +1,6 @@ -using System; -using AutoBogus; -using Bogus; +using AutoBogus; using Deepgram.Models; -using Deepgram.Tests.Fakers; using Deepgram.Tests.Fakes; -using Xunit; namespace Deepgram.Tests.ClientTests { @@ -94,8 +90,8 @@ private static DeepgramClient GetDeepgramClient(T returnObject) var mockIApiRequest = MockIApiRequest.Create(returnObject); var credentials = new CredentialsFaker().Generate(); var SUT = new DeepgramClient(credentials); - SUT.Keys.ApiRequest = mockIApiRequest.Object; - SUT.Keys.RequestMessageBuilder = mockIRequestMessageBuilder.Object; + SUT.Keys.ApiRequest = mockIApiRequest; + SUT.Keys.RequestMessageBuilder = mockIRequestMessageBuilder; return SUT; } diff --git a/Deepgram.Tests/ClientTests/PrerecordedTranscriptionTests.cs b/Deepgram.Tests/ClientTests/PrerecordedTranscriptionTests.cs index 0505d384..366e7a27 100644 --- a/Deepgram.Tests/ClientTests/PrerecordedTranscriptionTests.cs +++ b/Deepgram.Tests/ClientTests/PrerecordedTranscriptionTests.cs @@ -1,10 +1,7 @@ using AutoBogus; -using Bogus; using Deepgram.Models; using Deepgram.Request; -using Deepgram.Tests.Fakers; using Deepgram.Tests.Fakes; -using Xunit; namespace Deepgram.Tests.ClientTests { @@ -61,7 +58,7 @@ public async void Should_Return_A_Summary_Short_When_Summarize_Set_To_v2() var SUT = GetDeepgramClient(responseObject); responseObject.Results.Summary.Short = "This is a test summary"; - var client = FakeHttpMessageHandler.CreateHttpClientWithResult(responseObject); + var client = MockHttpClient.CreateHttpClientWithResult(responseObject); var fakeOptions = new PrerecordedTranscriptionOptions() { Summarize = "v2" @@ -88,7 +85,7 @@ public async void Should_Return_A_Summary_Short_When_Summarize_Set_To_bool(bool var responseObject = new AutoFaker().Generate(); var SUT = GetDeepgramClient(responseObject); responseObject.Results.Summary.Short = null; - var client = FakeHttpMessageHandler.CreateHttpClientWithResult(responseObject); + var client = MockHttpClient.CreateHttpClientWithResult(responseObject); var fakeOptions = new PrerecordedTranscriptionOptions() { Summarize = value @@ -113,8 +110,8 @@ private static DeepgramClient GetDeepgramClient(T returnObject) var mockIApiRequest = MockIApiRequest.Create(returnObject); var credentials = new CredentialsFaker().Generate(); var SUT = new DeepgramClient(credentials); - SUT.Transcription.Prerecorded.ApiRequest = mockIApiRequest.Object; - SUT.Transcription.Prerecorded.RequestMessageBuilder = mockIRequestMessageBuilder.Object; + SUT.Transcription.Prerecorded.ApiRequest = mockIApiRequest; + SUT.Transcription.Prerecorded.RequestMessageBuilder = mockIRequestMessageBuilder; return SUT; } } diff --git a/Deepgram.Tests/ClientTests/ProjectClientTests.cs b/Deepgram.Tests/ClientTests/ProjectClientTests.cs index cd9d4c1b..f076bc73 100644 --- a/Deepgram.Tests/ClientTests/ProjectClientTests.cs +++ b/Deepgram.Tests/ClientTests/ProjectClientTests.cs @@ -1,9 +1,6 @@ using AutoBogus; -using Bogus; using Deepgram.Models; -using Deepgram.Tests.Fakers; using Deepgram.Tests.Fakes; -using Xunit; namespace Deepgram.Tests.ClientTests { @@ -170,8 +167,8 @@ private static DeepgramClient GetDeepgramClient(T returnObject) var mockIApiRequest = MockIApiRequest.Create(returnObject); var credentials = new CredentialsFaker().Generate(); var SUT = new DeepgramClient(credentials); - SUT.Projects.ApiRequest = mockIApiRequest.Object; - SUT.Projects.RequestMessageBuilder = mockIRequestMessageBuilder.Object; + SUT.Projects.ApiRequest = mockIApiRequest; + SUT.Projects.RequestMessageBuilder = mockIRequestMessageBuilder; return SUT; } } diff --git a/Deepgram.Tests/ClientTests/UsageClientTests.cs b/Deepgram.Tests/ClientTests/UsageClientTests.cs index 2fcfe80f..7876b43e 100644 --- a/Deepgram.Tests/ClientTests/UsageClientTests.cs +++ b/Deepgram.Tests/ClientTests/UsageClientTests.cs @@ -1,9 +1,6 @@ -using System; -using AutoBogus; +using AutoBogus; using Deepgram.Models; -using Deepgram.Tests.Fakers; using Deepgram.Tests.Fakes; -using Xunit; namespace Deepgram.Tests.ClientTests { @@ -88,8 +85,8 @@ private static DeepgramClient GetDeepgramClient(T returnObject) var mockIApiRequest = MockIApiRequest.Create(returnObject); var credentials = new CredentialsFaker().Generate(); var SUT = new DeepgramClient(credentials); - SUT.Usage.ApiRequest = mockIApiRequest.Object; - SUT.Usage.RequestMessageBuilder = mockIRequestMessageBuilder.Object; + SUT.Usage.ApiRequest = mockIApiRequest; + SUT.Usage.RequestMessageBuilder = mockIRequestMessageBuilder; return SUT; } diff --git a/Deepgram.Tests/Deepgram.Tests.csproj b/Deepgram.Tests/Deepgram.Tests.csproj index 22865525..ed8b2ca6 100644 --- a/Deepgram.Tests/Deepgram.Tests.csproj +++ b/Deepgram.Tests/Deepgram.Tests.csproj @@ -6,15 +6,16 @@ - - - - - + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/Deepgram.Tests/Fakes/FakeHttpMessageHandler.cs b/Deepgram.Tests/Fakes/FakeHttpMessageHandler.cs deleted file mode 100644 index 42a738f9..00000000 --- a/Deepgram.Tests/Fakes/FakeHttpMessageHandler.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Net; -using System.Net.Http; -using System.Threading; -using System.Threading.Tasks; -using Bogus; -using Moq; -using Moq.Protected; -using Newtonsoft.Json; - -namespace Deepgram.Tests.Fakes -{ - public static class FakeHttpMessageHandler - { - public static Mock CreateMessageHandlerWithResult( - T result, HttpStatusCode code = HttpStatusCode.OK) - { - var messageHandler = new Mock(); - messageHandler.Protected() - .Setup>( - "SendAsync", - ItExpr.IsAny(), - ItExpr.IsAny()) - .ReturnsAsync(new HttpResponseMessage() - { - StatusCode = code, - Content = new StringContent(JsonConvert.SerializeObject(result)), - }); - - return messageHandler; - } - - public static HttpClient CreateHttpClientWithResult( - T result, HttpStatusCode code = HttpStatusCode.OK) - { - var httpClient = new HttpClient(CreateMessageHandlerWithResult(result, code).Object) - { - BaseAddress = new Uri(new Faker().Internet.Url()), - }; - - return httpClient; - } - } -} \ No newline at end of file diff --git a/Deepgram.Tests/Fakes/MockHttpClient.cs b/Deepgram.Tests/Fakes/MockHttpClient.cs new file mode 100644 index 00000000..265fc8cb --- /dev/null +++ b/Deepgram.Tests/Fakes/MockHttpClient.cs @@ -0,0 +1,17 @@ + +namespace Deepgram.Tests.Fakes +{ + public static class MockHttpClient + { + public static HttpClient CreateHttpClientWithResult( + T result, HttpStatusCode code = HttpStatusCode.OK) + { + var httpClient = new HttpClient(new MockHttpMessageHandler(result, code)) + { + BaseAddress = new Uri(new Faker().Internet.Url()), + }; + + return httpClient; + } + } +} \ No newline at end of file diff --git a/Deepgram.Tests/Fakes/MockHttpMessageHandler.cs b/Deepgram.Tests/Fakes/MockHttpMessageHandler.cs new file mode 100644 index 00000000..35516422 --- /dev/null +++ b/Deepgram.Tests/Fakes/MockHttpMessageHandler.cs @@ -0,0 +1,22 @@ +using Newtonsoft.Json; + +namespace Deepgram.Tests.Fakes; +public class MockHttpMessageHandler : HttpMessageHandler +{ + private readonly object _response; + private readonly HttpStatusCode _statusCode; + public MockHttpMessageHandler(object response, HttpStatusCode statusCode) + { + _response = response; + _statusCode = statusCode; + } + + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + return new HttpResponseMessage() + { + StatusCode = _statusCode, + Content = new StringContent(JsonConvert.SerializeObject(_response)) + }; + } +} diff --git a/Deepgram.Tests/Fakes/MockIApiRequest.cs b/Deepgram.Tests/Fakes/MockIApiRequest.cs index f9820300..e737a8ef 100644 --- a/Deepgram.Tests/Fakes/MockIApiRequest.cs +++ b/Deepgram.Tests/Fakes/MockIApiRequest.cs @@ -1,15 +1,11 @@ -using System.Net.Http; -using Deepgram.Interfaces; -using Moq; - -namespace Deepgram.Tests.Fakes +namespace Deepgram.Tests.Fakes { public class MockIApiRequest { - public static Mock Create(T returnObject) + public static IApiRequest Create(T returnObject) { - var mockAPiRequest = new Mock(); - mockAPiRequest.Setup(x => x.SendHttpRequestAsync(It.IsAny())).ReturnsAsync(returnObject); + var mockAPiRequest = Substitute.For(); + mockAPiRequest.SendHttpRequestAsync(Arg.Any()).Returns(returnObject); return mockAPiRequest; } } diff --git a/Deepgram.Tests/Fakes/MockIRequestMessageBuilder.cs b/Deepgram.Tests/Fakes/MockIRequestMessageBuilder.cs index 4bae9125..52074ae5 100644 --- a/Deepgram.Tests/Fakes/MockIRequestMessageBuilder.cs +++ b/Deepgram.Tests/Fakes/MockIRequestMessageBuilder.cs @@ -1,21 +1,19 @@ -using System.Net.Http; -using Deepgram.Interfaces; -using Deepgram.Models; -using Moq; +using Deepgram.Models; + namespace Deepgram.Tests.Fakes { public class MockIRequestMessageBuilder { - public static Mock Create() + public static IRequestMessageBuilder Create() { - var mockRequestMessageBuilder = new Mock(); - mockRequestMessageBuilder.Setup(x => x.CreateHttpRequestMessage( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny())).Returns(new HttpRequestMessage()); + var mockRequestMessageBuilder = Substitute.For(); + mockRequestMessageBuilder.CreateHttpRequestMessage( + Arg.Any(), + Arg.Any(), + Arg.Any(), + Arg.Any(), + Arg.Any()).Returns(new HttpRequestMessage()); return mockRequestMessageBuilder; } } diff --git a/Deepgram.Tests/GlobalUsings.cs b/Deepgram.Tests/GlobalUsings.cs new file mode 100644 index 00000000..6fe8cc00 --- /dev/null +++ b/Deepgram.Tests/GlobalUsings.cs @@ -0,0 +1,15 @@ +global using System; +global using System.Collections.Generic; +global using System.Linq; +global using System.Net; +global using System.Net.Http; +global using System.Text; +global using System.Threading; +global using System.Threading.Tasks; +global using Bogus; +global using Deepgram.Clients; +global using Deepgram.Interfaces; +global using Deepgram.Tests.Fakers; +global using Deepgram.Utilities; +global using NSubstitute; +global using Xunit; diff --git a/Deepgram.Tests/RequestTests/ApiRequestTests.cs b/Deepgram.Tests/RequestTests/ApiRequestTests.cs index cbccda31..363a015b 100644 --- a/Deepgram.Tests/RequestTests/ApiRequestTests.cs +++ b/Deepgram.Tests/RequestTests/ApiRequestTests.cs @@ -15,7 +15,7 @@ public async void Should_Return_A_Valid_Object_When_Deserialized() { //Arrange var responseObject = new AutoFaker().Generate(); - var client = FakeHttpMessageHandler.CreateHttpClientWithResult(responseObject); + var client = MockHttpClient.CreateHttpClientWithResult(responseObject); var SUT = new ApiRequest(client); //Act