Reputation: 8337
Trying to mock the following interface, but finding the syntax really awkward to work with.
public interface IKvpStoreRepository
{
string this[string key] { get; set; }
Task<bool> ContainsKey(string key);
}
Now I'm hoping to record the value into a backing store as below:
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] Expression tree may not contain assignment.
[2] How do I get both key and value?
Upvotes: 0
Views: 55
Reputation: 73253
This test passes.
[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");
}
Upvotes: 1