I have next interface
public interface IMyInterface
{
string this[string key] { get; set; }
}
and i want implement get/set in my test
var _Nvp = //...
var mockMyInterface = new Mock<IMyInterface>();
mockMyInterface
.Setup(e => e[It.IsAny<string>()])
.Returns((string key) => _Nvp[key]);
mockMyInterface
.SetupSet(c => c[It.IsAny<string>()] = It.IsAny<string>())
.Callback((string key, string value) => { _Nvp[key] = value; }));
But it does not work.. No errors, no messages..
var oj = mockMyInterface.Object;
oj["key"] = "value";
var value = oj["key"];
Variable value is always null.
Check out the following SO comment.
It seems there is a limitation on Moq's side resolving c[It.IsAny<string>()] on the SetupSet. It seems to work when specific keys are specified.
In your case you might want to go with a Stub with internal state implementing your interface rather than a mock.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With