Moq Xunit 测试设置为 return C# 中的 IDictionary

Moq Xunit test setup to return IDictionary in C#

我正在尝试为一种方法设置最小起订量:

IDictionary<string, string> MyMethod(myObject request);

在我的测试案例中,最小起订量设置如下:

mockServiceAdapter.Setup(x => x.MyMethod(TestData.getmyObject)).Returns(new Dictionary<string, string> { { "key1", "val1" } });

当我的测试执行 MyMethod() 时,它返回 null 但我期待 { "key1", "val1" }。我想念的地方。

你能帮帮我吗

这里的问题可能出在传递给模拟方法 (MyMethod) 的参数上。您可以尝试使用以下代码片段来代替传递对象 "TestData.getmyObject"。

mockServiceAdapter.Setup(x => x.MyMethod(It.IsAny<ClassName>())).Returns(new Dictionary<string, string> { { "key1", "val1" } });