Skip to content

Commit 9b23cd0

Browse files
CopilotCopilot
andcommitted
Replace Mock<McpServer> with real McpServer instances in McpServerPromptTests
Replace all Moq-based Mock<McpServer> usages with real McpServer instances created via McpServer.Create() using TestServerTransport. This removes the Moq dependency from this test file and uses concrete implementations instead. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 12ba337 commit 9b23cd0

1 file changed

Lines changed: 25 additions & 23 deletions

File tree

tests/ModelContextProtocol.Tests/Server/McpServerPromptTests.cs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Microsoft.Extensions.DependencyInjection;
33
using ModelContextProtocol.Protocol;
44
using ModelContextProtocol.Server;
5-
using Moq;
5+
using ModelContextProtocol.Tests.Utils;
66
using System.ComponentModel;
77
using System.Reflection;
88
using System.Runtime.InteropServices;
@@ -13,6 +13,9 @@ namespace ModelContextProtocol.Tests.Server;
1313

1414
public class McpServerPromptTests
1515
{
16+
private static McpServer CreateTestServer(IServiceProvider? services = null) =>
17+
McpServer.Create(new TestServerTransport(), new McpServerOptions(), serviceProvider: services);
18+
1619
private static JsonRpcRequest CreateTestJsonRpcRequest()
1720
{
1821
return new JsonRpcRequest
@@ -43,18 +46,18 @@ public void Create_InvalidArgs_Throws()
4346
[Fact]
4447
public async Task SupportsMcpServer()
4548
{
46-
Mock<McpServer> mockServer = new();
49+
McpServer server = CreateTestServer();
4750

48-
McpServerPrompt prompt = McpServerPrompt.Create((McpServer server) =>
51+
McpServerPrompt prompt = McpServerPrompt.Create((McpServer s) =>
4952
{
50-
Assert.Same(mockServer.Object, server);
53+
Assert.Same(server, s);
5154
return new ChatMessage(ChatRole.User, "Hello");
5255
});
5356

54-
Assert.DoesNotContain("server", prompt.ProtocolPrompt.Arguments?.Select(a => a.Name) ?? []);
57+
Assert.DoesNotContain("s", prompt.ProtocolPrompt.Arguments?.Select(a => a.Name) ?? []);
5558

5659
var result = await prompt.GetAsync(
57-
new RequestContext<GetPromptRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()),
60+
new RequestContext<GetPromptRequestParams>(server, CreateTestJsonRpcRequest()),
5861
TestContext.Current.CancellationToken);
5962
Assert.NotNull(result);
6063
Assert.NotNull(result.Messages);
@@ -71,8 +74,7 @@ public async Task SupportsCtorInjection()
7174
sc.AddSingleton(expectedMyService);
7275
IServiceProvider services = sc.BuildServiceProvider();
7376

74-
Mock<McpServer> mockServer = new();
75-
mockServer.SetupGet(s => s.Services).Returns(services);
77+
McpServer server = CreateTestServer(services);
7678

7779
MethodInfo? testMethod = typeof(HasCtorWithSpecialParameters).GetMethod(nameof(HasCtorWithSpecialParameters.TestPrompt));
7880
Assert.NotNull(testMethod);
@@ -83,7 +85,7 @@ public async Task SupportsCtorInjection()
8385
}, new() { Services = services });
8486

8587
var result = await prompt.GetAsync(
86-
new RequestContext<GetPromptRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()),
88+
new RequestContext<GetPromptRequestParams>(server, CreateTestJsonRpcRequest()),
8789
TestContext.Current.CancellationToken);
8890
Assert.NotNull(result);
8991
Assert.NotNull(result.Messages);
@@ -133,11 +135,11 @@ public async Task SupportsServiceFromDI()
133135
Assert.DoesNotContain("actualMyService", prompt.ProtocolPrompt.Arguments?.Select(a => a.Name) ?? []);
134136

135137
await Assert.ThrowsAnyAsync<ArgumentException>(async () => await prompt.GetAsync(
136-
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
138+
new RequestContext<GetPromptRequestParams>(CreateTestServer(), CreateTestJsonRpcRequest()),
137139
TestContext.Current.CancellationToken));
138140

139141
var result = await prompt.GetAsync(
140-
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()) { Services = services },
142+
new RequestContext<GetPromptRequestParams>(CreateTestServer(), CreateTestJsonRpcRequest()) { Services = services },
141143
TestContext.Current.CancellationToken);
142144
Assert.Equal("Hello", Assert.IsType<TextContentBlock>(result.Messages[0].Content).Text);
143145
}
@@ -158,7 +160,7 @@ public async Task SupportsOptionalServiceFromDI()
158160
}, new() { Services = services });
159161

160162
var result = await prompt.GetAsync(
161-
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
163+
new RequestContext<GetPromptRequestParams>(CreateTestServer(), CreateTestJsonRpcRequest()),
162164
TestContext.Current.CancellationToken);
163165
Assert.Equal("Hello", Assert.IsType<TextContentBlock>(result.Messages[0].Content).Text);
164166
}
@@ -171,7 +173,7 @@ public async Task SupportsDisposingInstantiatedDisposableTargets()
171173
_ => new DisposablePromptType());
172174

173175
var result = await prompt1.GetAsync(
174-
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
176+
new RequestContext<GetPromptRequestParams>(CreateTestServer(), CreateTestJsonRpcRequest()),
175177
TestContext.Current.CancellationToken);
176178
Assert.Equal("disposals:1", Assert.IsType<TextContentBlock>(result.Messages[0].Content).Text);
177179
}
@@ -184,7 +186,7 @@ public async Task SupportsAsyncDisposingInstantiatedAsyncDisposableTargets()
184186
_ => new AsyncDisposablePromptType());
185187

186188
var result = await prompt1.GetAsync(
187-
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
189+
new RequestContext<GetPromptRequestParams>(CreateTestServer(), CreateTestJsonRpcRequest()),
188190
TestContext.Current.CancellationToken);
189191
Assert.Equal("asyncDisposals:1", Assert.IsType<TextContentBlock>(result.Messages[0].Content).Text);
190192
}
@@ -197,7 +199,7 @@ public async Task SupportsAsyncDisposingInstantiatedAsyncDisposableAndDisposable
197199
_ => new AsyncDisposableAndDisposablePromptType());
198200

199201
var result = await prompt1.GetAsync(
200-
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
202+
new RequestContext<GetPromptRequestParams>(CreateTestServer(), CreateTestJsonRpcRequest()),
201203
TestContext.Current.CancellationToken);
202204
Assert.Equal("disposals:0, asyncDisposals:1", Assert.IsType<TextContentBlock>(result.Messages[0].Content).Text);
203205
}
@@ -213,7 +215,7 @@ public async Task CanReturnGetPromptResult()
213215
});
214216

215217
var actual = await prompt.GetAsync(
216-
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
218+
new RequestContext<GetPromptRequestParams>(CreateTestServer(), CreateTestJsonRpcRequest()),
217219
TestContext.Current.CancellationToken);
218220

219221
Assert.Same(expected, actual);
@@ -230,7 +232,7 @@ public async Task CanReturnText()
230232
});
231233

232234
var actual = await prompt.GetAsync(
233-
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
235+
new RequestContext<GetPromptRequestParams>(CreateTestServer(), CreateTestJsonRpcRequest()),
234236
TestContext.Current.CancellationToken);
235237

236238
Assert.NotNull(actual);
@@ -256,7 +258,7 @@ public async Task CanReturnPromptMessage()
256258
});
257259

258260
var actual = await prompt.GetAsync(
259-
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
261+
new RequestContext<GetPromptRequestParams>(CreateTestServer(), CreateTestJsonRpcRequest()),
260262
TestContext.Current.CancellationToken);
261263

262264
Assert.NotNull(actual);
@@ -288,7 +290,7 @@ public async Task CanReturnPromptMessages()
288290
});
289291

290292
var actual = await prompt.GetAsync(
291-
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
293+
new RequestContext<GetPromptRequestParams>(CreateTestServer(), CreateTestJsonRpcRequest()),
292294
TestContext.Current.CancellationToken);
293295

294296
Assert.NotNull(actual);
@@ -315,7 +317,7 @@ public async Task CanReturnChatMessage()
315317
});
316318

317319
var actual = await prompt.GetAsync(
318-
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
320+
new RequestContext<GetPromptRequestParams>(CreateTestServer(), CreateTestJsonRpcRequest()),
319321
TestContext.Current.CancellationToken);
320322

321323
Assert.NotNull(actual);
@@ -347,7 +349,7 @@ public async Task CanReturnChatMessages()
347349
});
348350

349351
var actual = await prompt.GetAsync(
350-
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
352+
new RequestContext<GetPromptRequestParams>(CreateTestServer(), CreateTestJsonRpcRequest()),
351353
TestContext.Current.CancellationToken);
352354

353355
Assert.NotNull(actual);
@@ -368,7 +370,7 @@ public async Task ThrowsForNullReturn()
368370
});
369371

370372
await Assert.ThrowsAsync<InvalidOperationException>(async () => await prompt.GetAsync(
371-
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
373+
new RequestContext<GetPromptRequestParams>(CreateTestServer(), CreateTestJsonRpcRequest()),
372374
TestContext.Current.CancellationToken));
373375
}
374376

@@ -381,7 +383,7 @@ public async Task ThrowsForUnexpectedTypeReturn()
381383
});
382384

383385
await Assert.ThrowsAsync<InvalidOperationException>(async () => await prompt.GetAsync(
384-
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
386+
new RequestContext<GetPromptRequestParams>(CreateTestServer(), CreateTestJsonRpcRequest()),
385387
TestContext.Current.CancellationToken));
386388
}
387389

0 commit comments

Comments
 (0)