diff --git a/Source/Mockolate.SourceGenerators/Sources/Sources.MemberIds.cs b/Source/Mockolate.SourceGenerators/Sources/Sources.MemberIds.cs index 7ebd32289..12952ef07 100644 --- a/Source/Mockolate.SourceGenerators/Sources/Sources.MemberIds.cs +++ b/Source/Mockolate.SourceGenerators/Sources/Sources.MemberIds.cs @@ -280,21 +280,26 @@ internal void AddMethod(Method method) MethodIds[method] = id; } - internal void Emit(StringBuilder sb, string indent) + /// + /// is the expression for the id of the first member, for a mock that + /// shares its registry with another mock and must not reuse the ids of that mock. + /// + internal void Emit(StringBuilder sb, string indent, string? idOffset = null) { + string offsetPrefix = idOffset is null ? "" : idOffset + " + "; if (_declarations.Count == 0) { - sb.Append(indent).Append("internal const int MemberCount = 0;").AppendLine(); + sb.Append(indent).Append("internal const int MemberCount = ").Append(offsetPrefix).Append("0;").AppendLine(); return; } for (int i = 0; i < _declarations.Count; i++) { sb.Append(indent).Append("internal const int ").Append(_declarations[i]) - .Append(" = ").Append(i).Append(';').AppendLine(); + .Append(" = ").Append(offsetPrefix).Append(i).Append(';').AppendLine(); } - sb.Append(indent).Append("internal const int MemberCount = ").Append(_declarations.Count) + sb.Append(indent).Append("internal const int MemberCount = ").Append(offsetPrefix).Append(_declarations.Count) .Append(';').AppendLine(); foreach ((Property property, string fieldName) in _propertyGetterAccessFields) diff --git a/Source/Mockolate.SourceGenerators/Sources/Sources.MockClass.cs b/Source/Mockolate.SourceGenerators/Sources/Sources.MockClass.cs index ce4e597e3..a26efdf13 100644 --- a/Source/Mockolate.SourceGenerators/Sources/Sources.MockClass.cs +++ b/Source/Mockolate.SourceGenerators/Sources/Sources.MockClass.cs @@ -133,7 +133,7 @@ public static string MockClass( sb.Append("\t\tglobal::Mockolate.IMock").AppendLine(); sb.Append("\t{").AppendLine(); - memberIds.Emit(sb, "\t\t"); + memberIds.Emit(sb, "\t\t", @class is { ClassFullName: "global::System.Net.Http.HttpClient", } ? "global::Mockolate.Mock.HttpMessageHandler.MemberCount" : null); sb.AppendLine(); AppendCreateFastInteractions(sb, "\t\t"); diff --git a/Tests/Mockolate.SourceGenerators.Tests/Snapshot/MockGenerationSnapshotTests/GeneratorOutput_MatchesExpectedSnapshot/Mock.HttpClient.g.awexpect-snapshot.cs b/Tests/Mockolate.SourceGenerators.Tests/Snapshot/MockGenerationSnapshotTests/GeneratorOutput_MatchesExpectedSnapshot/Mock.HttpClient.g.awexpect-snapshot.cs index 705978059..66fecfae6 100644 --- a/Tests/Mockolate.SourceGenerators.Tests/Snapshot/MockGenerationSnapshotTests/GeneratorOutput_MatchesExpectedSnapshot/Mock.HttpClient.g.awexpect-snapshot.cs +++ b/Tests/Mockolate.SourceGenerators.Tests/Snapshot/MockGenerationSnapshotTests/GeneratorOutput_MatchesExpectedSnapshot/Mock.HttpClient.g.awexpect-snapshot.cs @@ -21,10 +21,10 @@ internal class HttpClient : global::System.Net.Http.HttpClient, IMockForHttpClient, IMockSetupForHttpClient, IMockProtectedSetupForHttpClient, global::Mockolate.MockExtensionsForHttpClient.IMockSetupInitializationForHttpClient, IMockVerifyForHttpClient, IMockProtectedVerifyForHttpClient, global::Mockolate.IMock { - internal const int MemberId_Send = 0; - internal const int MemberId_SendAsync = 1; - internal const int MemberId_Dispose = 2; - internal const int MemberCount = 3; + internal const int MemberId_Send = global::Mockolate.Mock.HttpMessageHandler.MemberCount + 0; + internal const int MemberId_SendAsync = global::Mockolate.Mock.HttpMessageHandler.MemberCount + 1; + internal const int MemberId_Dispose = global::Mockolate.Mock.HttpMessageHandler.MemberCount + 2; + internal const int MemberCount = global::Mockolate.Mock.HttpMessageHandler.MemberCount + 3; /// /// Creates a FastMockInteractions sized to MemberCount for use as the mock's interaction store. diff --git a/Tests/Mockolate.Tests/Web/HttpClientTests.cs b/Tests/Mockolate.Tests/Web/HttpClientTests.cs index c0e02bc31..33349916c 100644 --- a/Tests/Mockolate.Tests/Web/HttpClientTests.cs +++ b/Tests/Mockolate.Tests/Web/HttpClientTests.cs @@ -26,6 +26,59 @@ public async Task HttpClientMock_WithHandler_ShouldUseSharedRegistry() await That(client2.Mock.VerifySetup(setup)).Once(); } + [Test] + public async Task SendAsync_ShouldBeCountedSeparatelyForTheClientAndItsHandler() + { + HttpMessageHandler handler = HttpMessageHandler.CreateMock(); + HttpClient client = HttpClient.CreateMock([handler,]); + client.Mock.Setup + .GetAsync(It.IsUri("testably.org")) + .ReturnsAsync(HttpStatusCode.Accepted); + + await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, "https://testably.org"), CancellationToken.None); + await client.GetAsync("https://testably.org", CancellationToken.None); + + await That(client.Mock.Verify.SendAsync(It.IsAny(), It.IsAny())).Once() + .Because("only the first request goes through the virtual SendAsync of the client"); + await That(handler.Mock.VerifyProtected.SendAsync(It.IsAny(), It.IsAny())).Twice() + .Because("both requests reach the handler"); + await That(client.Mock.Verify.GetAsync(It.IsUri("testably.org"))).Twice(); + } + + [Test] + public async Task SendAsync_WithoutExplicitHandler_ShouldOnlyCountTheCallsOnTheClient() + { + HttpClient client = HttpClient.CreateMock(); + client.Mock.Setup + .GetAsync(It.IsUri("testably.org")) + .ReturnsAsync(HttpStatusCode.Accepted); + + await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, "https://testably.org"), CancellationToken.None); + await client.GetAsync("https://testably.org", CancellationToken.None); + + await That(client.Mock.Verify.SendAsync(It.IsAny(), It.IsAny())).Once() + .Because("only the first request goes through the virtual SendAsync of the client"); + await That(client.Mock.Verify.GetAsync(It.IsUri("testably.org"))).Twice(); + } + + [Test] + public async Task SetupOfSendAsyncOnTheClient_ShouldNotAnswerTheHandler() + { + HttpMessageHandler handler = HttpMessageHandler.CreateMock(); + HttpClient client = HttpClient.CreateMock([handler,]); + client.Mock.Setup + .GetAsync(It.IsUri("testably.org")) + .ReturnsAsync(HttpStatusCode.Accepted); + client.Mock.Setup + .SendAsync(It.IsAny(), It.IsAny()) + .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.BadRequest)); + + HttpResponseMessage response = await client.GetAsync("https://testably.org", CancellationToken.None); + + await That(response.StatusCode).IsEqualTo(HttpStatusCode.Accepted) + .Because("GetAsync does not go through the virtual SendAsync of the client, so only the setup for the handler applies"); + } + [Test] public async Task WithDifferentBehavior_ShouldThrow() {