Skip to content

Commit c9c34d0

Browse files
authored
refactor: replace Moq with Mockolate (#1405)
This PR refactors the testing infrastructure by replacing the [Moq](https://github.com/devlooped/moq) mocking framework with [Mockolate](https://github.com/aweXpect/Mockolate). The change affects package dependencies and updates test code to use Mockolate's API syntax throughout the codebase. ### Key Changes: - Replaced Moq package dependency with Mockolate version 1.0.0 - Updated test code to use Mockolate's API syntax (Mock.Create, SetupMock, etc.) - Updated README documentation to reflect the new mocking framework
1 parent fc2f666 commit c9c34d0

File tree

4 files changed

+40
-28
lines changed

4 files changed

+40
-28
lines changed

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<PackageVersion Include="coverlet.collector" Version="6.0.4" />
2323
<PackageVersion Include="GitHubActionsTestLogger" Version="3.0.1" />
2424
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.0" />
25-
<PackageVersion Include="Moq" Version="4.20.72" />
25+
<PackageVersion Include="Mockolate" Version="1.0.0" />
2626
<PackageVersion Include="NUnit" Version="4.4.0" />
2727
<PackageVersion Include="NUnit.Analyzers" Version="4.11.2" />
2828
<PackageVersion Include="NUnit3TestAdapter" Version="6.1.0" />

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,27 +101,27 @@ void MyFancyMethod()
101101

102102
### Mock support
103103

104-
Since version 4.0 the top-level APIs expose interfaces instead of abstract base classes (these still exist, though), allowing you to completely mock the file system. Here's a small example, using [Moq](https://github.com/moq/moq4):
104+
Since version 4.0 the top-level APIs expose interfaces instead of abstract base classes (these still exist, though), allowing you to completely mock the file system. Here's a small example, using [Mockolate](https://github.com/aweXpect/Mockolate):
105105

106106
```csharp
107107
[Test]
108108
public void Test1()
109109
{
110-
var watcher = Mock.Of<IFileSystemWatcher>();
111-
var file = Mock.Of<IFile>();
110+
var watcher = Mock.Create<IFileSystemWatcher>();
111+
var file = Mock.Create<IFile>();
112112

113-
Mock.Get(file).Setup(f => f.Exists(It.IsAny<string>())).Returns(true);
114-
Mock.Get(file).Setup(f => f.ReadAllText(It.IsAny<string>())).Throws<OutOfMemoryException>();
113+
file.SetupMock.Method.Exists(It.IsAny<string>()).Returns(true);
114+
file.SetupMock.Method.ReadAllText(It.IsAny<string>()).Throws<OutOfMemoryException>();
115115

116116
var unitUnderTest = new SomeClassUsingFileSystemWatcher(watcher, file);
117117

118118
Assert.Throws<OutOfMemoryException>(() => {
119-
Mock.Get(watcher).Raise(w => w.Created += null, new System.IO.FileSystemEventArgs(System.IO.WatcherChangeTypes.Created, @"C:\Some\Directory", "Some.File"));
119+
watcher.RaiseOnMock.Created(null, new System.IO.FileSystemEventArgs(System.IO.WatcherChangeTypes.Created, @"C:\Some\Directory", "Some.File"));
120120
});
121121

122-
Mock.Get(file).Verify(f => f.Exists(It.IsAny<string>()), Times.Once);
122+
file.VerifyMock.Invoked.Exists(It.IsAny<string>()).Once();
123123

124-
Assert.True(unitUnderTest.FileWasCreated);
124+
Assert.That(unitUnderTest.FileWasCreated, Is.True);
125125
}
126126

127127
public class SomeClassUsingFileSystemWatcher

tests/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</PackageReference>
1919
<PackageReference Include="GitHubActionsTestLogger" />
2020
<PackageReference Include="Microsoft.NET.Test.Sdk" />
21-
<PackageReference Include="Moq" />
21+
<PackageReference Include="Mockolate" />
2222
<PackageReference Include="NUnit" />
2323
<PackageReference Include="NUnit.Analyzers">
2424
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace System.IO.Abstractions.Tests;
1+
#if !NET6_0
2+
using Mockolate;
3+
4+
namespace System.IO.Abstractions.Tests;
25

36
[TestFixture]
47
public class FileSystemTests
@@ -24,80 +27,89 @@ await That(memoryStream).HasLength().GreaterThan(0)
2427
[Test]
2528
public async Task Mock_File_Succeeds()
2629
{
27-
var fileSystemMock = new Moq.Mock<IFileSystem>();
30+
var fileSystemMock = Mock.Create<IFileSystem>(fs =>
31+
fs.Property.File.InitializeWith(Mock.Create<IFile>()));
2832

2933
await That(() =>
30-
fileSystemMock.Setup(x => x.File.ToString()).Returns("")
34+
fileSystemMock.File.SetupMock.Method.ReadAllText(It.IsAny<string>()).Returns("")
3135
).DoesNotThrow();
3236
}
3337

3438
[Test]
3539
public async Task Mock_Directory_Succeeds()
3640
{
37-
var fileSystemMock = new Moq.Mock<IFileSystem>();
41+
var fileSystemMock = Mock.Create<IFileSystem>(fs =>
42+
fs.Property.Directory.InitializeWith(Mock.Create<IDirectory>()));
3843

3944
await That(() =>
40-
fileSystemMock.Setup(x => x.Directory.ToString()).Returns("")
45+
fileSystemMock.Directory.SetupMock.Method.CreateDirectory(It.IsAny<string>())
4146
).DoesNotThrow();
4247
}
4348

4449
[Test]
4550
public async Task Mock_FileInfo_Succeeds()
4651
{
47-
var fileSystemMock = new Moq.Mock<IFileSystem>();
52+
var fileSystemMock = Mock.Create<IFileSystem>(fs =>
53+
fs.Property.FileInfo.InitializeWith(Mock.Create<IFileInfoFactory>()));
4854

4955
await That(() =>
50-
fileSystemMock.Setup(x => x.FileInfo.ToString()).Returns("")
56+
fileSystemMock.FileInfo.SetupMock.Method.New(It.IsAny<string>())
5157
).DoesNotThrow();
5258
}
5359

5460
[Test]
5561
public async Task Mock_FileStream_Succeeds()
5662
{
57-
var fileSystemMock = new Moq.Mock<IFileSystem>();
63+
var fileSystemMock = Mock.Create<IFileSystem>(fs =>
64+
fs.Property.FileStream.InitializeWith(Mock.Create<IFileStreamFactory>()));
5865

5966
await That(() =>
60-
fileSystemMock.Setup(x => x.FileStream.ToString()).Returns("")
67+
fileSystemMock.FileStream.SetupMock.Method.New(It.IsAny<string>(), It.IsAny<FileMode>())
6168
).DoesNotThrow();
6269
}
6370

6471
[Test]
6572
public async Task Mock_Path_Succeeds()
6673
{
67-
var fileSystemMock = new Moq.Mock<IFileSystem>();
74+
var fileSystemMock = Mock.Create<IFileSystem>(fs =>
75+
fs.Property.Path.InitializeWith(Mock.Create<IPath>()));
6876

6977
await That(() =>
70-
fileSystemMock.Setup(x => x.Path.ToString()).Returns("")
78+
fileSystemMock.Path.SetupMock.Method.Combine(It.IsAny<string>(), It.IsAny<string>())
7179
).DoesNotThrow();
7280
}
7381

7482
[Test]
7583
public async Task Mock_DirectoryInfo_Succeeds()
7684
{
77-
var fileSystemMock = new Moq.Mock<IFileSystem>();
85+
var fileSystemMock = Mock.Create<IFileSystem>(fs =>
86+
fs.Property.DirectoryInfo.InitializeWith(Mock.Create<IDirectoryInfoFactory>()));
7887

7988
await That(() =>
80-
fileSystemMock.Setup(x => x.DirectoryInfo.ToString()).Returns("")
89+
fileSystemMock.DirectoryInfo.SetupMock.Method.New(It.IsAny<string>())
8190
).DoesNotThrow();
8291
}
8392

8493
[Test]
8594
public async Task Mock_DriveInfo_Succeeds()
8695
{
87-
var fileSystemMock = new Moq.Mock<IFileSystem>();
96+
var fileSystemMock = Mock.Create<IFileSystem>(fs =>
97+
fs.Property.DriveInfo.InitializeWith(Mock.Create<IDriveInfoFactory>()));
8898

8999
await That(() =>
90-
fileSystemMock.Setup(x => x.DirectoryInfo.ToString()).Returns("")
100+
fileSystemMock.DriveInfo.SetupMock.Method.New(It.IsAny<string>())
91101
).DoesNotThrow();
92102
}
93103

94104
[Test]
95105
public async Task Mock_FileSystemWatcher_Succeeds()
96106
{
97-
var fileSystemMock = new Moq.Mock<IFileSystem>();
107+
var fileSystemMock = Mock.Create<IFileSystem>(fs =>
108+
fs.Property.FileSystemWatcher.InitializeWith(Mock.Create<IFileSystemWatcherFactory>()));
98109

99110
await That(() =>
100-
fileSystemMock.Setup(x => x.FileSystemWatcher.ToString()).Returns("")
111+
fileSystemMock.FileSystemWatcher.SetupMock.Method.New(It.IsAny<string>())
101112
).DoesNotThrow();
102113
}
103-
}
114+
}
115+
#endif

0 commit comments

Comments
 (0)