Skip to content

Commit

Permalink
Replace Moq with NSubstitute (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
hf-kklein authored Aug 10, 2023
1 parent 3a1decb commit fc1e3ac
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
2 changes: 1 addition & 1 deletion EDILibraryTests/EDILibraryTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="Moq" Version="4.20.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 4 additions & 3 deletions EDILibraryTests/EdifactFormatVersionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using EDILibrary;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using NSubstitute;

namespace EDILibraryTests
{
Expand Down Expand Up @@ -128,9 +129,9 @@ private EdifactFormatVersion ActualCode(IEdifactFormatVersionProvider versionPro
[TestMethod]
public void TestMockingVersionProvider()
{
var versionProviderMock = new Moq.Mock<IEdifactFormatVersionProvider>();
versionProviderMock.Setup(vp => vp.GetCurrent()).Returns(EdifactFormatVersion.FV1904);
Assert.AreEqual(EdifactFormatVersion.FV1904, ActualCode(versionProviderMock.Object));
var versionProviderMock = Substitute.For<IEdifactFormatVersionProvider>();
versionProviderMock.GetCurrent().Returns(EdifactFormatVersion.FV1904);
Assert.AreEqual(EdifactFormatVersion.FV1904, ActualCode(versionProviderMock));
}

/// <summary>
Expand Down
35 changes: 15 additions & 20 deletions EDILibraryTests/TemplateLoaderExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using EDILibrary;
using EDILibrary.Interfaces;
using EDILibrary.MAUS;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using NSubstitute;


namespace EDILibraryTests
Expand All @@ -20,16 +19,14 @@ public class TemplateLoaderExtensionTests
[TestMethod]
public async Task TestLoadingRegularTemplateIfExists()
{
var loaderMock = new Mock<ITemplateLoader>();
loaderMock.Setup(l => l.LoadMausTemplate(EdifactFormat.MSCONS, EdifactFormatVersion.FV2304, "13002")).ReturnsAsync(new Anwendungshandbuch()).Verifiable();
var loaderMock = Substitute.For<ITemplateLoader>();
loaderMock.LoadMausTemplate(EdifactFormat.MSCONS, EdifactFormatVersion.FV2304, "13002").Returns(Task.FromResult(new Anwendungshandbuch()));

var loaderUnderTest = loaderMock.Object;
var (actualMaus, actualFormatVersion) = await loaderUnderTest.LoadMausTemplateOrFallback(EdifactFormat.MSCONS, EdifactFormatVersion.FV2304, "13002");
var (actualMaus, actualFormatVersion) = await loaderMock.LoadMausTemplateOrFallback(EdifactFormat.MSCONS, EdifactFormatVersion.FV2304, "13002");
actualMaus.Should().NotBeNull();
actualFormatVersion.Should().Be(EdifactFormatVersion.FV2304);

loaderMock.VerifyAll();
loaderMock.VerifyNoOtherCalls();
loaderMock.Received();
}

/// <summary>
Expand All @@ -38,18 +35,17 @@ public async Task TestLoadingRegularTemplateIfExists()
[TestMethod]
public async Task TestLoadingFallbackTemplateIfExists()
{
var loaderMock = new Mock<ITemplateLoader>();
loaderMock.Setup(l => l.LoadMausTemplate(EdifactFormat.MSCONS, EdifactFormatVersion.FV2304, "13002")).ReturnsAsync((Anwendungshandbuch)null).Verifiable();
loaderMock.Setup(l => l.LoadMausTemplate(EdifactFormat.MSCONS, EdifactFormatVersion.FV2210, "13002")).ReturnsAsync((Anwendungshandbuch)null).Verifiable();
loaderMock.Setup(l => l.LoadMausTemplate(EdifactFormat.MSCONS, EdifactFormatVersion.FV2110, "13002")).ReturnsAsync(new Anwendungshandbuch()).Verifiable();
var loaderMock = Substitute.For<ITemplateLoader>();
loaderMock.LoadMausTemplate(EdifactFormat.MSCONS, EdifactFormatVersion.FV2304, "13002").Returns(Task.FromResult((Anwendungshandbuch)null));
loaderMock.LoadMausTemplate(EdifactFormat.MSCONS, EdifactFormatVersion.FV2210, "13002").Returns(Task.FromResult((Anwendungshandbuch)null));
loaderMock.LoadMausTemplate(EdifactFormat.MSCONS, EdifactFormatVersion.FV2110, "13002").Returns(Task.FromResult(new Anwendungshandbuch()));

var loaderUnderTest = loaderMock.Object;
var loaderUnderTest = loaderMock;
var (actualMaus, actualFormatVersion) = await loaderUnderTest.LoadMausTemplateOrFallback(EdifactFormat.MSCONS, EdifactFormatVersion.FV2304, "13002");
actualMaus.Should().NotBeNull();
actualFormatVersion.Should().Be(EdifactFormatVersion.FV2110);

loaderMock.VerifyAll();
loaderMock.VerifyNoOtherCalls();
loaderMock.Received();
}

/// <summary>
Expand All @@ -58,16 +54,15 @@ public async Task TestLoadingFallbackTemplateIfExists()
[TestMethod]
public async Task TestLoadingNothingIfNotEvenAFallbackExists()
{
var loaderMock = new Mock<ITemplateLoader>();
loaderMock.Setup(l => l.LoadMausTemplate(EdifactFormat.MSCONS, It.IsAny<EdifactFormatVersion>(), "13002")).ReturnsAsync((Anwendungshandbuch)null).Verifiable();
var loaderMock = Substitute.For<ITemplateLoader>();
loaderMock.LoadMausTemplate(EdifactFormat.MSCONS, Arg.Any<EdifactFormatVersion>(), "13002").Returns(Task.FromResult((Anwendungshandbuch)null));


var loaderUnderTest = loaderMock.Object;
var (actualMaus, actualFormatVersion) = await loaderUnderTest.LoadMausTemplateOrFallback(EdifactFormat.MSCONS, EdifactFormatVersion.FV2304, "13002");
var (actualMaus, actualFormatVersion) = await loaderMock.LoadMausTemplateOrFallback(EdifactFormat.MSCONS, EdifactFormatVersion.FV2304, "13002");
actualMaus.Should().BeNull();
actualFormatVersion.Should().BeNull();

loaderMock.VerifyAll();
loaderMock.ReceivedCalls();
}
}
}

0 comments on commit fc1e3ac

Please sign in to comment.