在问题的结尾: Using Moq to set indexers in C#,有人强调了我也遇到的问题。但他们没有找到解决办法。
具体来说,我正在尝试使用泛型It.IsAny<string>
作为键的匹配器并通过It.IsAny<object>
. 通过索引访问并设置值时,它永远不会匹配,也不会访问我的回调方法。所以我的单元测试失败了。
var stateTable = new HashTable;
var httpSession = new Mock<HttpSessionStateBase>();
//works via httpSession.Add(key, value);
httpSession.Setup(x => x.Add(It.IsAny<string>(), It.IsAny<object>()))
.Callback((string index, object value) => {
var i = index;
var v = value;
stateData[i] = v;
});
//does not work via httpSession[key] = value;
httpSession.SetupSet(x => x[It.IsAny<string>()] = It.IsAny<object>())
.Callback( (string index, object value) => {
var i = index;
var v = value;
stateData[i] = v;
});
我正在使用最小起订量 4.0.10827