From d62348f3b7e4c206e14cfb3283562662add72fa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20NIVET?= <5648584+mnivet@users.noreply.github.com> Date: Thu, 24 Oct 2024 11:47:00 +0200 Subject: [PATCH 1/2] Upgrade to Moq 4.20.69 that added new methods on IVerifies Also update target frameworks and tests dependencies --- .../Moq.SetupAsync.Tests.csproj | 8 ++-- Moq.SetupAsync.sln | 6 +-- Moq.SetupAsync/Language/Flow/SetupAsync.cs | 40 +++++++++++++++++++ Moq.SetupAsync/Moq.SetupAsync.csproj | 6 +-- 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/Moq.SetupAsync.Tests/Moq.SetupAsync.Tests.csproj b/Moq.SetupAsync.Tests/Moq.SetupAsync.Tests.csproj index 045db38..7f7c763 100644 --- a/Moq.SetupAsync.Tests/Moq.SetupAsync.Tests.csproj +++ b/Moq.SetupAsync.Tests/Moq.SetupAsync.Tests.csproj @@ -1,15 +1,15 @@  - net46 + net462;net6.0 false - - - + + + diff --git a/Moq.SetupAsync.sln b/Moq.SetupAsync.sln index 2fd993a..7dc9f8e 100644 --- a/Moq.SetupAsync.sln +++ b/Moq.SetupAsync.sln @@ -1,11 +1,11 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27703.2000 +# Visual Studio Version 17 +VisualStudioVersion = 17.11.35327.3 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Moq.SetupAsync", "Moq.SetupAsync\Moq.SetupAsync.csproj", "{274FBF44-A58E-4464-B6D0-089742C4944A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Moq.SetupAsync.Tests", "Moq.SetupAsync.Tests\Moq.SetupAsync.Tests.csproj", "{3B85E7AA-2C71-419B-9894-2AA790B604FC}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Moq.SetupAsync.Tests", "Moq.SetupAsync.Tests\Moq.SetupAsync.Tests.csproj", "{3B85E7AA-2C71-419B-9894-2AA790B604FC}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/Moq.SetupAsync/Language/Flow/SetupAsync.cs b/Moq.SetupAsync/Language/Flow/SetupAsync.cs index 00d5f1e..567f755 100644 --- a/Moq.SetupAsync/Language/Flow/SetupAsync.cs +++ b/Moq.SetupAsync/Language/Flow/SetupAsync.cs @@ -165,6 +165,26 @@ public void Verifiable(string failMessage) { this.Setup.Verifiable(failMessage); } + + public void Verifiable(Times times) + { + this.Setup.Verifiable(times); + } + + public void Verifiable(Func times) + { + this.Setup.Verifiable(times); + } + + public void Verifiable(Times times, string failMessage) + { + this.Setup.Verifiable(times, failMessage); + } + + public void Verifiable(Func times, string failMessage) + { + this.Setup.Verifiable(times, failMessage); + } } public class SetupAsync : ISetupAsync @@ -325,5 +345,25 @@ public void Verifiable(string failMessage) { this.Setup.Verifiable(failMessage); } + + public void Verifiable(Times times) + { + this.Setup.Verifiable(times); + } + + public void Verifiable(Func times) + { + this.Setup.Verifiable(times); + } + + public void Verifiable(Times times, string failMessage) + { + this.Setup.Verifiable(times, failMessage); + } + + public void Verifiable(Func times, string failMessage) + { + this.Setup.Verifiable(times, failMessage); + } } } diff --git a/Moq.SetupAsync/Moq.SetupAsync.csproj b/Moq.SetupAsync/Moq.SetupAsync.csproj index 84cba54..09f6717 100644 --- a/Moq.SetupAsync/Moq.SetupAsync.csproj +++ b/Moq.SetupAsync/Moq.SetupAsync.csproj @@ -3,13 +3,13 @@ Talentsoft.Moq.SetupAsync Moq.SetupAsync - 1.0.0 + 1.1.0 Mickaël Nivet Talentsoft Extension for Moq that allow to easily mock async methods https://github.com/TalentSoft/Moq.SetupAsync moq async task - net45;net46;netstandard1.3 + net462;netstandard2.0;netstandard2.1;net6.0 Moq @@ -23,7 +23,7 @@ - + From 8651e96560c340067805e78fedc66fe12af0b24a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20NIVET?= <5648584+mnivet@users.noreply.github.com> Date: Thu, 24 Oct 2024 11:56:01 +0200 Subject: [PATCH 2/2] Rewrite unit tests to be async --- .../SetupAsyncActionFixture.cs | 28 +++++++-------- Moq.SetupAsync.Tests/SetupAsyncFuncFixture.cs | 36 +++++++++---------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/Moq.SetupAsync.Tests/SetupAsyncActionFixture.cs b/Moq.SetupAsync.Tests/SetupAsyncActionFixture.cs index 76516d0..621f9f3 100644 --- a/Moq.SetupAsync.Tests/SetupAsyncActionFixture.cs +++ b/Moq.SetupAsync.Tests/SetupAsyncActionFixture.cs @@ -7,48 +7,48 @@ namespace Moq.SetupAsync.Tests public class SetupAsyncActionFixture { [Fact] - public void SetupAsyncActionIsWaitable() + public async Task SetupAsyncActionCanBeAwaited() { var mock = new Mock(); mock.SetupAsync(x => x.DoActionAsync()); - mock.Object.DoActionAsync().Wait(); + await mock.Object.DoActionAsync(); mock.VerifyAll(); } [Fact] - public void SetupAsyncActionWithOneArgIsWaitable() + public async Task SetupAsyncActionWithOneArgCanBeAwaited() { var mock = new Mock(); mock.SetupAsync(x => x.DoActionAsync(It.IsAny())); - mock.Object.DoActionAsync("any string").Wait(); + await mock.Object.DoActionAsync("any string"); mock.VerifyAll(); } [Fact] - public void SetupAsyncActionIsVerifiable() + public async Task SetupAsyncActionIsVerifiable() { var mock = new Mock(); mock.SetupAsync(x => x.DoActionAsync()).Verifiable(); mock.Verify(x => x.DoActionAsync(), Times.Never); - mock.Object.DoActionAsync().Wait(); + await mock.Object.DoActionAsync(); mock.Verify(x => x.DoActionAsync(), Times.Once); mock.Verify(); } [Fact] - public void SetupAsyncActionWithOneArgIsVerifiable() + public async Task SetupAsyncActionWithOneArgIsVerifiable() { var mock = new Mock(); mock.SetupAsync(x => x.DoActionAsync(It.IsAny())).Verifiable(); mock.Verify(x => x.DoActionAsync("any string"), Times.Never); - mock.Object.DoActionAsync("any string").Wait(); + await mock.Object.DoActionAsync("any string"); mock.Verify(x => x.DoActionAsync("any string"), Times.Once); mock.Verify(); @@ -65,37 +65,37 @@ public void SetupAsyncActionIsVerifiableAndErrorMessageCanBeSpecified() } [Fact] - public void SetupAsyncActionWithCallback() + public async Task SetupAsyncActionWithCallback() { var mock = new Mock(); var isCalled = false; mock.SetupAsync(x => x.DoActionAsync()).Callback(() => isCalled = true); - mock.Object.DoActionAsync().Wait(); + await mock.Object.DoActionAsync(); Assert.True(isCalled); } [Fact] - public void SetupAsyncActionWithOneArgWithCallback() + public async Task SetupAsyncActionWithOneArgWithCallback() { var mock = new Mock(); var isCalled = false; mock.SetupAsync(x => x.DoActionAsync(It.IsAny())).Callback(s => isCalled = s == "any string"); - mock.Object.DoActionAsync("any string").Wait(); + await mock.Object.DoActionAsync("any string"); Assert.True(isCalled); } [Fact] - public void SetupAsyncActionWithThrows() + public async Task SetupAsyncActionWithThrows() { var mock = new Mock(); mock.SetupAsync(x => x.DoActionAsync()).Throws(); var task = mock.Object.DoActionAsync(); - Assert.ThrowsAsync(() => task).Wait(); + await Assert.ThrowsAsync(() => task); } public interface IFoo diff --git a/Moq.SetupAsync.Tests/SetupAsyncFuncFixture.cs b/Moq.SetupAsync.Tests/SetupAsyncFuncFixture.cs index 7c76024..84da66a 100644 --- a/Moq.SetupAsync.Tests/SetupAsyncFuncFixture.cs +++ b/Moq.SetupAsync.Tests/SetupAsyncFuncFixture.cs @@ -7,48 +7,48 @@ namespace Moq.SetupAsync.Tests public class SetupAsyncFuncFixture { [Fact] - public void SetupAsyncFuncIsWaitable() + public async Task SetupAsyncFuncCanBeAwaited() { var mock = new Mock(); mock.SetupAsync(x => x.ExecFuncAsync()); - mock.Object.ExecFuncAsync().Wait(); + await mock.Object.ExecFuncAsync(); mock.VerifyAll(); } [Fact] - public void SetupAsyncFuncWithOneArgIsWaitable() + public async Task SetupAsyncFuncWithOneArgCanBeAwaited() { var mock = new Mock(); mock.SetupAsync(x => x.ExecFuncAsync(It.IsAny())); - mock.Object.ExecFuncAsync("any string").Wait(); + await mock.Object.ExecFuncAsync("any string"); mock.VerifyAll(); } [Fact] - public void SetupAsyncFuncIsVerifiable() + public async Task SetupAsyncFuncIsVerifiable() { var mock = new Mock(); mock.SetupAsync(x => x.ExecFuncAsync()).Verifiable(); mock.Verify(x => x.ExecFuncAsync(), Times.Never); - mock.Object.ExecFuncAsync().Wait(); + await mock.Object.ExecFuncAsync(); mock.Verify(x => x.ExecFuncAsync(), Times.Once); mock.Verify(); } [Fact] - public void SetupAsyncFuncWithOneArgIsVerifiable() + public async Task SetupAsyncFuncWithOneArgIsVerifiable() { var mock = new Mock(); mock.SetupAsync(x => x.ExecFuncAsync(It.IsAny())).Verifiable(); mock.Verify(x => x.ExecFuncAsync("any string"), Times.Never); - mock.Object.ExecFuncAsync("any string").Wait(); + await mock.Object.ExecFuncAsync("any string"); mock.Verify(x => x.ExecFuncAsync("any string"), Times.Once); mock.Verify(); @@ -65,25 +65,25 @@ public void SetupAsyncFuncIsVerifiableAndErrorMessageCanBeSpecified() } [Fact] - public void SetupAsyncFuncReturnsNullByDefault() + public async Task SetupAsyncFuncReturnsNullByDefault() { var mock = new Mock(); mock.SetupAsync(x => x.ExecFuncAsync()); - Assert.Null(mock.Object.ExecFuncAsync().Result); + Assert.Null(await mock.Object.ExecFuncAsync()); } [Fact] - public void SetupAsyncFuncWithStaticReturns() + public async Task SetupAsyncFuncWithStaticReturns() { var mock = new Mock(); mock.SetupAsync(x => x.ExecFuncAsync()).Returns("custom return"); - Assert.Equal("custom return", mock.Object.ExecFuncAsync().Result); + Assert.Equal("custom return", await mock.Object.ExecFuncAsync()); } [Fact] - public void SetupAsyncFuncWithComputedReturns() + public async Task SetupAsyncFuncWithComputedReturns() { var mock = new Mock(); var isCalled = false; @@ -93,29 +93,29 @@ public void SetupAsyncFuncWithComputedReturns() return "custom return"; }); - Assert.Equal("custom return", mock.Object.ExecFuncAsync().Result); + Assert.Equal("custom return", await mock.Object.ExecFuncAsync()); Assert.True(isCalled); } [Fact] - public void SetupAsyncFuncWithOneArgWithReturnsThatDependsOnArg() + public async Task SetupAsyncFuncWithOneArgWithReturnsThatDependsOnArg() { var mock = new Mock(); mock.SetupAsync(x => x.ExecFuncAsync(It.IsAny())).Returns(s => "hello " + s); - var result = mock.Object.ExecFuncAsync("any string").Result; + var result = await mock.Object.ExecFuncAsync("any string"); Assert.Equal("hello any string", result); } [Fact] - public void SetupAsyncFuncWithThrows() + public async Task SetupAsyncFuncWithThrows() { var mock = new Mock(); mock.SetupAsync(x => x.ExecFuncAsync()).Throws(); var task = mock.Object.ExecFuncAsync(); - Assert.ThrowsAsync(() => task).Wait(); + await Assert.ThrowsAsync(() => task); } public interface IFoo