Skip to content

Commit 2f26ac2

Browse files
committed
Use FakeItEasy instead of Moq
1 parent 12a0b9c commit 2f26ac2

File tree

3 files changed

+43
-66
lines changed

3 files changed

+43
-66
lines changed

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<PackageVersion Include="AWSSDK.Core" Version="4.0.0.2" />
1717
<PackageVersion Include="AWSSDK.SQS" Version="4.0.0.1" />
1818
<PackageVersion Include="AWSSDK.S3" Version="4.0.0.1" />
19-
<PackageVersion Include="Moq" Version="4.20.72" />
19+
<PackageVersion Include="FakeItEasy" Version="8.3.0" />
2020
<PackageVersion Include="Elastic.Ingest.Elasticsearch" Version="0.11.3" />
2121
</ItemGroup>
2222
<!-- Build -->

tests/docs-assembler.Tests/src/docs-assembler.Tests/DocsSyncTests.cs

Lines changed: 41 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
// See the LICENSE file in the project root for more information
44

55
using System.IO.Abstractions.TestingHelpers;
6-
using Amazon.Runtime;
76
using Amazon.S3;
87
using Amazon.S3.Transfer;
98
using Documentation.Assembler.Deploying;
109
using Elastic.Documentation.Diagnostics;
1110
using Elastic.Markdown.IO;
11+
using FakeItEasy;
1212
using FluentAssertions;
1313
using Microsoft.Extensions.Logging;
14-
using Moq;
1514

1615
namespace Documentation.Assembler.Tests;
1716

@@ -23,7 +22,7 @@ public async Task TestPlan()
2322
// Arrange
2423
IReadOnlyCollection<IDiagnosticsOutput> diagnosticsOutputs = [];
2524
var collector = new DiagnosticsCollector(diagnosticsOutputs);
26-
var mockS3Client = new Mock<IAmazonS3>();
25+
var mockS3Client = A.Fake<IAmazonS3>();
2726
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
2827
{
2928
{ "docs/add1.md", new MockFileData("# New Document 1") },
@@ -37,31 +36,28 @@ public async Task TestPlan()
3736
});
3837

3938
var context = new AssembleContext("dev", collector, fileSystem, fileSystem, null, Path.Combine(Paths.WorkingDirectoryRoot.FullName, ".artifacts", "assembly"));
40-
41-
mockS3Client.Setup(client => client.ListObjectsV2Async(
42-
It.IsAny<Amazon.S3.Model.ListObjectsV2Request>(),
43-
It.IsAny<Cancel>()
44-
)).ReturnsAsync(new Amazon.S3.Model.ListObjectsV2Response
45-
{
46-
S3Objects =
47-
[
48-
new Amazon.S3.Model.S3Object
49-
{
50-
Key = "docs/delete.md",
51-
},
52-
new Amazon.S3.Model.S3Object
53-
{
54-
Key = "docs/skip.md",
55-
ETag = "\"69048c0964c9577a399b138b706a467a\"" // This is the result of CalculateS3ETag
56-
},
57-
new Amazon.S3.Model.S3Object
58-
{
59-
Key = "docs/update.md",
60-
ETag = "\"existing-etag\""
61-
}
62-
]
63-
});
64-
var planStrategy = new AwsS3SyncPlanStrategy(mockS3Client.Object, "fake", context, new LoggerFactory());
39+
A.CallTo(() => mockS3Client.ListObjectsV2Async(A<Amazon.S3.Model.ListObjectsV2Request>._, A<Cancel>._))
40+
.Returns(new Amazon.S3.Model.ListObjectsV2Response
41+
{
42+
S3Objects =
43+
[
44+
new Amazon.S3.Model.S3Object
45+
{
46+
Key = "docs/delete.md",
47+
},
48+
new Amazon.S3.Model.S3Object
49+
{
50+
Key = "docs/skip.md",
51+
ETag = "\"69048c0964c9577a399b138b706a467a\""
52+
}, // This is the result of CalculateS3ETag
53+
new Amazon.S3.Model.S3Object
54+
{
55+
Key = "docs/update.md",
56+
ETag = "\"existing-etag\""
57+
}
58+
]
59+
});
60+
var planStrategy = new AwsS3SyncPlanStrategy(mockS3Client, "fake", context, new LoggerFactory());
6561

6662
// Act
6763
var plan = await planStrategy.Plan(Cancel.None);
@@ -88,11 +84,8 @@ public async Task TestApply()
8884
// Arrange
8985
IReadOnlyCollection<IDiagnosticsOutput> diagnosticsOutputs = [];
9086
var collector = new DiagnosticsCollector(diagnosticsOutputs);
91-
92-
93-
var mockS3Client = new Mock<IAmazonS3>();
94-
var mockTransferUtility = new Mock<ITransferUtility>();
95-
87+
var moxS3Client = A.Fake<IAmazonS3>();
88+
var moxTransferUtility = A.Fake<ITransferUtility>();
9689
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
9790
{
9891
{ "docs/add1.md", new MockFileData("# New Document 1") },
@@ -104,9 +97,7 @@ public async Task TestApply()
10497
{
10598
CurrentDirectory = Path.Combine(Paths.WorkingDirectoryRoot.FullName, ".artifacts", "assembly")
10699
});
107-
108100
var context = new AssembleContext("dev", collector, fileSystem, fileSystem, null, Path.Combine(Paths.WorkingDirectoryRoot.FullName, ".artifacts", "assembly"));
109-
110101
var plan = new SyncPlan
111102
{
112103
Count = 6,
@@ -128,26 +119,18 @@ public async Task TestApply()
128119
{ DestinationPath = "docs/delete.md" }
129120
]
130121
};
131-
132-
mockS3Client.Setup(client => client.DeleteObjectsAsync(
133-
It.IsAny<Amazon.S3.Model.DeleteObjectsRequest>(),
134-
It.IsAny<Cancel>()
135-
)).ReturnsAsync(new Amazon.S3.Model.DeleteObjectsResponse
136-
{
137-
HttpStatusCode = System.Net.HttpStatusCode.OK
138-
});
139-
122+
A.CallTo(() => moxS3Client.DeleteObjectsAsync(A<Amazon.S3.Model.DeleteObjectsRequest>._, A<Cancel>._))
123+
.Returns(new Amazon.S3.Model.DeleteObjectsResponse
124+
{
125+
HttpStatusCode = System.Net.HttpStatusCode.OK
126+
});
140127
var transferredFiles = Array.Empty<string>();
141-
142-
mockTransferUtility.Setup(utility => utility.UploadDirectoryAsync(
143-
It.IsAny<TransferUtilityUploadDirectoryRequest>(),
144-
It.IsAny<Cancel>()
145-
)).Callback<TransferUtilityUploadDirectoryRequest, Cancel>((request, _) =>
146-
{
147-
transferredFiles = context.ReadFileSystem.Directory.GetFiles(request.Directory, request.SearchPattern, request.SearchOption);
148-
});
149-
150-
var applier = new AwsS3SyncApplyStrategy(mockS3Client.Object, mockTransferUtility.Object, "fake", context, new LoggerFactory(), collector);
128+
A.CallTo(() => moxTransferUtility.UploadDirectoryAsync(A<TransferUtilityUploadDirectoryRequest>._, A<Cancel>._))
129+
.Invokes((TransferUtilityUploadDirectoryRequest request, Cancel _) =>
130+
{
131+
transferredFiles = fileSystem.Directory.GetFiles(request.Directory, request.SearchPattern, request.SearchOption);
132+
});
133+
var applier = new AwsS3SyncApplyStrategy(moxS3Client, moxTransferUtility, "fake", context, new LoggerFactory(), collector);
151134

152135
// Act
153136
await applier.Apply(plan, Cancel.None);
@@ -156,16 +139,10 @@ public async Task TestApply()
156139
transferredFiles.Length.Should().Be(4); // 3 add requests + 1 update request
157140
transferredFiles.Should().NotContain("docs/skip.md");
158141

159-
mockS3Client.Verify(client => client.DeleteObjectsAsync(
160-
It.Is<Amazon.S3.Model.DeleteObjectsRequest>(req => req.Objects.Any(o => o.Key == "docs/delete.md")),
161-
It.IsAny<Cancel>()), Times.Once);
142+
A.CallTo(() => moxS3Client.DeleteObjectsAsync(A<Amazon.S3.Model.DeleteObjectsRequest>._, A<Cancel>._))
143+
.MustHaveHappenedOnceExactly();
162144

163-
mockTransferUtility.Verify(utility => utility.UploadDirectoryAsync(
164-
It.Is<TransferUtilityUploadDirectoryRequest>(req =>
165-
req.BucketName == "fake" &&
166-
req.SearchPattern == "*" &&
167-
req.SearchOption == SearchOption.AllDirectories &&
168-
req.UploadFilesConcurrently),
169-
It.IsAny<Cancel>()), Times.Once);
145+
A.CallTo(() => moxTransferUtility.UploadDirectoryAsync(A<TransferUtilityUploadDirectoryRequest>._, A<Cancel>._))
146+
.MustHaveHappenedOnceExactly();
170147
}
171148
}

tests/docs-assembler.Tests/src/docs-assembler.Tests/docs-assembler.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
</PropertyGroup>
1717

1818
<ItemGroup>
19+
<PackageReference Include="FakeItEasy" />
1920
<PackageReference Include="Microsoft.NET.Test.Sdk" />
20-
<PackageReference Include="Moq" />
2121
<PackageReference Include="xunit.v3" />
2222
<PackageReference Include="xunit.runner.visualstudio" />
2323
<PackageReference Include="GitHubActionsTestLogger" />

0 commit comments

Comments
 (0)