2016-02-14 63 views
0

试图模拟下面的接口,但发现语法真的很难处理。如何重新设置MOQ呼叫?

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]如何获取密钥和值?

回答

1

此测试通过。

[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"); 
} 
+0

斯卡德,感谢您的回复。你能澄清一下,如果我可以使用变量例如。 'x [It.IsAny ()>]'?我想让它通过。 – Alwyn