如何重新路由最小起订量电话?

How to reroute MOQ call?

尝试模拟以下接口,但发现语法真的很难用。

public interface IKvpStoreRepository
{
    string this[string key] { get; set; }

    Task<bool> ContainsKey(string key);
}

现在我希望将值记录到后备存储中,如下所示:

var backingStore = new Dictionary<string,string>();
var mockKvpRepository = new Mock<IKvpStoreRepository>();
mockKvpRepository.
    Setup(_ => _[It.IsAny<string>()] = It.IsAny<Task<string>>()) //BROKE [1]
    .Callback((key,value) => backingStore[key] = value) //??? [2]
    .ReturnsAsync("blah"); //??? [3]

[1] 表达式树可能不包含赋值。

[2] 如何同时获取键和值?

此测试通过。

[Test]
public void q35387809() {
    var backingStore = new Dictionary<string, string>();
    var mockKvpRepository = new Mock<IKvpStoreRepository>();

    mockKvpRepository.SetupSet(x => x["blah"] = It.IsAny<string>())
        .Callback((string name, string value) => { backingStore[name] = value; });

    mockKvpRepository.Object["blah"] = "foo";

    backingStore.Count.Should().Be(1);
    backingStore["blah"].Should().Be("foo");
}