2016-02-14 89 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