Skip to content

Commit

Permalink
Add proper mocking of HttpClient in HgServiceTests
Browse files Browse the repository at this point in the history
This allows us to uncomment the tests we were skipping earlier, because
now we can mock HttpClient and not allow HgService to hit the network
during unit tests.
  • Loading branch information
rmunn committed May 29, 2024
1 parent acd85b5 commit 28201a6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
19 changes: 14 additions & 5 deletions backend/Testing/LexCore/Services/HgServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Moq;
using Moq.Contrib.HttpClient;
using Shouldly;
using Testing.Fixtures;

Expand All @@ -30,8 +31,16 @@ public HgServiceTests()
HgResumableUrl = LexboxResumable,
SendReceiveDomain = LexboxHgWeb
};
var handler = new Mock<HttpMessageHandler>(MockBehavior.Strict);

// This may need to become more sophisticated if our FinishReset tests are changed to include
// a Mercurial repo with actual commits in it, but this is good enough at the moment.
var AllZeroHash = "0000000000000000000000000000000000000000";
handler.SetupAnyRequest().ReturnsResponse(AllZeroHash);

var mockFactory = handler.CreateClientFactory();
_hgService = new HgService(new OptionsWrapper<HgConfig>(_hgConfig),
Mock.Of<IHttpClientFactory>(),
mockFactory,
NullLogger<HgService>.Instance);
CleanUpTempDir();
}
Expand All @@ -53,9 +62,9 @@ public void DetermineProjectPrefixWorks(HgType type, string expectedUrl)
HgService.DetermineProjectUrlPrefix(type, _hgConfig).ShouldBe(expectedUrl);
}

// [Theory]
// [InlineData(".hg/important-file.bin")]
// [InlineData("unzip-test/.hg/important-file.bin")]
[Theory]
[InlineData(".hg/important-file.bin")]
[InlineData("unzip-test/.hg/important-file.bin")]
public async Task CanFinishResetByUnZippingAnArchive(string filePath)
{
// arrange
Expand Down Expand Up @@ -98,7 +107,7 @@ private void CreateSimpleEntry(ZipArchive zipArchive, string filePath)
fileStream.Flush();
}

// [Fact]
[Fact]
public async Task ThrowsIfNoHgFolderIsFound()
{
var code = "unzip-test-no-hg";
Expand Down
1 change: 1 addition & 0 deletions backend/Testing/Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<Choose>
<When Condition=" '$(MercurialVersion)' == '6' ">
<ItemGroup>
<PackageReference Include="Moq.Contrib.HttpClient" Version="1.4.0" />
<PackageReference Include="SIL.Chorus.LibChorus" Version="6.0.0-beta0046" />
<PackageReference Include="SIL.Chorus.Mercurial" Version="6.*" />
</ItemGroup>
Expand Down

0 comments on commit 28201a6

Please sign in to comment.