Skip to content
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
13 changes: 9 additions & 4 deletions Source/Mockolate.SourceGenerators/Sources/Sources.MemberIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,21 +280,26 @@ internal void AddMethod(Method method)
MethodIds[method] = id;
}

internal void Emit(StringBuilder sb, string indent)
/// <remarks>
/// <paramref name="idOffset" /> 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.
/// </remarks>
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions Tests/Mockolate.Tests/Web/HttpClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpRequestMessage>(), It.IsAny<CancellationToken>())).Once()
.Because("only the first request goes through the virtual SendAsync of the client");
await That(handler.Mock.VerifyProtected.SendAsync(It.IsAny<HttpRequestMessage>(), It.IsAny<CancellationToken>())).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<HttpRequestMessage>(), It.IsAny<CancellationToken>())).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<HttpRequestMessage>(), It.IsAny<CancellationToken>())
.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()
{
Expand Down
Loading