Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove moq #538

Merged
merged 3 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions tests/ImageSort.UnitTests/Actions/DeleteActionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.IO;
using ImageSort.Actions;
using ImageSort.FileSystem;
using Moq;
using NSubstitute;
using Xunit;

namespace ImageSort.UnitTests.Actions;
Expand All @@ -14,44 +14,40 @@ public void DeletesAndRestoresTheFileCorrectly()
{
const string fileToDelete = @"C:\Some File.png";

var fsMock = new Mock<IFileSystem>();
var recycleBinMock = new Mock<IRecycleBin>();
var fileRestorerMock = new Mock<IDisposable>();
var fsMock = Substitute.For<IFileSystem>();
var recycleBinMock = Substitute.For<IRecycleBin>();
var fileRestorerMock = Substitute.For<IDisposable>();

fsMock.Setup(fs => fs.FileExists(fileToDelete)).Returns(true).Verifiable();
fsMock.FileExists(fileToDelete).Returns(true);

fileRestorerMock.Setup(fr => fr.Dispose()).Verifiable();
recycleBinMock.Send(fileToDelete, false).Returns(fileRestorerMock);

recycleBinMock.Setup(recycleBin => recycleBin.Send(fileToDelete, false)).Returns(fileRestorerMock.Object)
.Verifiable();
var deleteAction = new DeleteAction(fileToDelete, fsMock, recycleBinMock);

var deleteAction = new DeleteAction(fileToDelete, fsMock.Object, recycleBinMock.Object);

fsMock.Verify(fs => fs.FileExists(fileToDelete));
fsMock.Received().FileExists(fileToDelete);

deleteAction.Act();

recycleBinMock.Verify(recycleBin => recycleBin.Send(fileToDelete, false));
recycleBinMock.Received().Send(fileToDelete, false);

deleteAction.Revert();

fileRestorerMock.Verify(fr => fr.Dispose());
fileRestorerMock.Received().Dispose();
}

[Fact(DisplayName = "Throws when the file to delete does not exist")]
public void ThrowsWhenTheFileDoesNotExist()
{
const string fileThatDoesntExist = @"C:\Fictional File.fake";

var fsMock = new Mock<IFileSystem>();
var recycleBinMock = new Mock<IRecycleBin>();
var fileRestorerMock = new Mock<IDisposable>();
var fsMock = Substitute.For<IFileSystem>();
var recycleBinMock = Substitute.For<IRecycleBin>();

fsMock.Setup(fs => fs.FileExists(fileThatDoesntExist)).Returns(false).Verifiable();
fsMock.FileExists(fileThatDoesntExist).Returns(false);

Assert.Throws<FileNotFoundException>(() =>
new DeleteAction(fileThatDoesntExist, fsMock.Object, recycleBinMock.Object));
new DeleteAction(fileThatDoesntExist, fsMock, recycleBinMock));

fsMock.Verify(fs => fs.FileExists(fileThatDoesntExist));
fsMock.Received().FileExists(fileThatDoesntExist);
}
}
34 changes: 16 additions & 18 deletions tests/ImageSort.UnitTests/Actions/MoveActionTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.IO;
using ImageSort.Actions;
using ImageSort.FileSystem;
using Moq;
using NSubstitute;
using Xunit;

namespace ImageSort.UnitTests.Actions;
Expand All @@ -18,27 +18,25 @@ public void FileGetsMovedCorrectly()
var notifedOfAction = false;
var notifiedOfReversion = false;

var fsMock = new Mock<IFileSystem>();
var fsMock = Substitute.For<IFileSystem>();

fsMock.Setup(fs => fs.Move(oldPath, newPath)).Verifiable();
fsMock.Setup(fs => fs.Move(newPath, oldPath)).Verifiable();
fsMock.Setup(fs => fs.FileExists(oldPath)).Returns(true);
fsMock.Setup(fs => fs.DirectoryExists(newFolder)).Returns(true);
fsMock.FileExists(oldPath).Returns(true);
fsMock.DirectoryExists(newFolder).Returns(true);

var moveAction = new MoveAction(oldPath, newFolder, fsMock.Object,
var moveAction = new MoveAction(oldPath, newFolder, fsMock,
(f, t) => notifedOfAction = true,
(f, t) => notifiedOfReversion = true);

fsMock.Verify(fs => fs.FileExists(oldPath));
fsMock.Verify(fs => fs.DirectoryExists(newFolder));
fsMock.Received().FileExists(oldPath);
fsMock.Received().DirectoryExists(newFolder);

moveAction.Act();

fsMock.Verify(fs => fs.Move(oldPath, newPath));
fsMock.Received().Move(oldPath, newPath);

moveAction.Revert();

fsMock.Verify(fs => fs.Move(newPath, oldPath));
fsMock.Received().Move(newPath, oldPath);

Assert.True(notifedOfAction, "The caller should be notified when an action acts.");
Assert.True(notifiedOfReversion, "The caller should be notified when an action is reverted.");
Expand All @@ -52,15 +50,15 @@ public void HandlesFileOrDirectoryNotExisting()
const string fakeDirectory = @"C:\DirectoryThatDoesntExist";
const string fakeFile = @"C:\SomeFakeFile.gif";

var fsMock = new Mock<IFileSystem>();
var fsMock = Substitute.For<IFileSystem>();

fsMock.Setup(fs => fs.DirectoryExists(existingDirectory)).Returns(true);
fsMock.Setup(fs => fs.FileExists(existingFile)).Returns(true);
fsMock.Setup(fs => fs.DirectoryExists(fakeDirectory)).Returns(false);
fsMock.Setup(fs => fs.FileExists(fakeFile)).Returns(false);
fsMock.DirectoryExists(existingDirectory).Returns(true);
fsMock.FileExists(existingFile).Returns(true);
fsMock.DirectoryExists(fakeDirectory).Returns(false);
fsMock.FileExists(fakeFile).Returns(false);

Assert.Throws<FileNotFoundException>(() => new MoveAction(fakeFile, existingDirectory, fsMock.Object));
Assert.Throws<FileNotFoundException>(() => new MoveAction(fakeFile, existingDirectory, fsMock));

Assert.Throws<DirectoryNotFoundException>(() => new MoveAction(existingFile, fakeDirectory, fsMock.Object));
Assert.Throws<DirectoryNotFoundException>(() => new MoveAction(existingFile, fakeDirectory, fsMock));
}
}
34 changes: 16 additions & 18 deletions tests/ImageSort.UnitTests/Actions/RenameActionTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.IO;
using ImageSort.Actions;
using ImageSort.FileSystem;
using Moq;
using NSubstitute;
using Xunit;

namespace ImageSort.UnitTests.Actions;
Expand All @@ -18,23 +18,21 @@ public void CanRenameFilesAndUndo()
var canAct = false;
var canRevert = false;

var fsMock = new Mock<IFileSystem>();
var fsMock = Substitute.For<IFileSystem>();

fsMock.Setup(fs => fs.FileExists(oldPath)).Returns(true);
fsMock.Setup(fs => fs.FileExists(newPath)).Returns(false);
fsMock.Setup(fs => fs.Move(oldPath, newPath)).Verifiable();
fsMock.Setup(fs => fs.Move(newPath, oldPath)).Verifiable();
fsMock.FileExists(oldPath).Returns(true);
fsMock.FileExists(newPath).Returns(false);

var renameAction = new RenameAction(oldPath, newFileName, fsMock.Object,
var renameAction = new RenameAction(oldPath, newFileName, fsMock,
(o, n) => canAct = true, (n, o) => canRevert = true);

renameAction.Act();

fsMock.Verify(fs => fs.Move(oldPath, newPath));
fsMock.Received().Move(oldPath, newPath);

renameAction.Revert();

fsMock.Verify(fs => fs.Move(newPath, oldPath));
fsMock.Received().Move(newPath, oldPath);

Assert.True(canAct);
Assert.True(canRevert);
Expand All @@ -50,18 +48,18 @@ public void ThrowsWhenFileDoesNotExistOrNewPathIsAlreadyUsed()
const string alreadyExistingName = @"already-exists";
const string alreadyExistingPath = @"C:\already-exists.png";

var fsMock = new Mock<IFileSystem>();
var fsMock = Substitute.For<IFileSystem>();

fsMock.Setup(fs => fs.FileExists(oldPath)).Returns(true);
fsMock.Setup(fs => fs.FileExists(newPath)).Returns(false);
fsMock.Setup(fs => fs.FileExists(invalidOldPath)).Returns(false).Verifiable();
fsMock.Setup(fs => fs.FileExists(alreadyExistingPath)).Returns(true).Verifiable();
fsMock.FileExists(oldPath).Returns(true);
fsMock.FileExists(newPath).Returns(false);
fsMock.FileExists(invalidOldPath).Returns(false);
fsMock.FileExists(alreadyExistingPath).Returns(true);

Assert.Throws<FileNotFoundException>(() => new RenameAction(invalidOldPath, newFileName, fsMock.Object));
Assert.Throws<FileNotFoundException>(() => new RenameAction(invalidOldPath, newFileName, fsMock));

Assert.Throws<IOException>(() => new RenameAction(oldPath, alreadyExistingName, fsMock.Object));
Assert.Throws<IOException>(() => new RenameAction(oldPath, alreadyExistingName, fsMock));

fsMock.Verify(fs => fs.FileExists(invalidOldPath));
fsMock.Verify(fs => fs.FileExists(alreadyExistingPath));
fsMock.Received().FileExists(invalidOldPath);
fsMock.Received().FileExists(alreadyExistingPath);
}
}
6 changes: 5 additions & 1 deletion tests/ImageSort.UnitTests/ImageSort.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="NSubstitute.Analyzers.CSharp" Version="1.0.16">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="ReactiveUI" Version="19.5.1" />
<PackageReference Include="ReactiveUI.Testing" Version="19.5.1" />
<PackageReference Include="xunit" Version="2.6.3" />
Expand Down
38 changes: 18 additions & 20 deletions tests/ImageSort.UnitTests/ViewModels/ActionsViewModelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Threading.Tasks;
using ImageSort.Actions;
using ImageSort.ViewModels;
using Moq;
using NSubstitute;
using Xunit;

namespace ImageSort.UnitTests.ViewModels;
Expand All @@ -20,13 +20,11 @@ public async Task WorksCorrectly()

var actionsVM = new ActionsViewModel();

var actionMock = new Mock<IReversibleAction>();
var actionMock = Substitute.For<IReversibleAction>();

actionMock.Setup(a => a.Act()).Verifiable();
actionMock.Setup(a => a.Revert()).Verifiable();
actionMock.SetupGet(a => a.DisplayName).Returns(actionDisplayName);
actionMock.DisplayName.Returns(actionDisplayName);

await actionsVM.Execute.Execute(actionMock.Object);
await actionsVM.Execute.Execute(actionMock);

Assert.Equal(actionDisplayName, actionsVM.LastDone);

Expand All @@ -44,8 +42,8 @@ public async Task WorksCorrectly()
Assert.Equal(actionDisplayName, actionsVM.LastDone);
Assert.NotEqual(actionDisplayName, actionsVM.LastUndone);

actionMock.Verify(a => a.Act(), Times.Exactly(2));
actionMock.Verify(a => a.Revert(), Times.Once);
actionMock.Received(2).Act();
actionMock.Received(1).Revert();

// make sure clearing works
await actionsVM.Clear.Execute();
Expand All @@ -60,28 +58,28 @@ public async Task WorksCorrectly()
public async Task NotifiesUserOfErrors()
{
// configure an action that fails when executed
var failingActMock = new Mock<IReversibleAction>();
var failingActMock = Substitute.For<IReversibleAction>();

failingActMock.Setup(a => a.Act()).Throws(new Exception("Act doesn't work"));
failingActMock.When(a => a.Act()).Do(x => { throw new Exception("Act doesn't work"); });

// configure an action that fails on reversion (on undo)
var failingRevertMock = new Mock<IReversibleAction>();
var failingRevertMock = Substitute.For<IReversibleAction>();

failingRevertMock.Setup(a => a.Revert()).Throws(new Exception("Revert doesn't work"));
failingRevertMock.Setup(a => a.Act());
failingRevertMock.When(a => a.Revert()).Do(x => { throw new Exception("Revert doesn't work"); });

// configure an action that fails on the second time being executed (on redo)
var failingActOnUndoMock = new Mock<IReversibleAction>();
var failingActOnUndoMock = Substitute.For<IReversibleAction>();

var timesCalled = 0;

failingActOnUndoMock.Setup(a => a.Revert());
failingActOnUndoMock.Setup(a => a.Act()).Callback(() =>
failingActOnUndoMock.When(a => a.Act()).Do(x =>
{
timesCalled = timesCalled switch
{
0 => 1,
_ => throw new Exception("Act doesn't work")
});
};
});

var actionsVM = new ActionsViewModel();

Expand All @@ -95,14 +93,14 @@ public async Task NotifiesUserOfErrors()
});

// fails on execute
await actionsVM.Execute.Execute(failingActMock.Object);
await actionsVM.Execute.Execute(failingActMock);

// fails on undo
await actionsVM.Execute.Execute(failingRevertMock.Object);
await actionsVM.Execute.Execute(failingRevertMock);
await actionsVM.Undo.Execute();

// fails on redo
await actionsVM.Execute.Execute(failingActOnUndoMock.Object);
await actionsVM.Execute.Execute(failingActOnUndoMock);
await actionsVM.Undo.Execute();
await actionsVM.Redo.Execute();

Expand Down
Loading
Loading