gpt4 book ai didi

asp.net-mvc-3 - MOQ - 模拟一个 Dictionary 对象

转载 作者:行者123 更新时间:2023-12-04 07:30:37 25 4
gpt4 key购买 nike

我有以下 Moq 设置:

... other code to setup bigMoq object ...
var innerMoq = new Mock<IDictionary<string, double>>();
innerMoq.SetupGet(d => d["COMPLEX"]).Returns(6d);
innerMoq.SetupGet(d => d["MEDIUM"]).Returns(8d);
innerMoq.SetupGet(d => d["SIMPLE"]).Returns(10d);
bigMoq.SetupGet(d => d.ComplexityWeights).Returns(x.Object);

在运行测试方法时,我传入 bigMoq 作为参数。

以下工作:
bigMoqVar.ComplexityWeights["COMPLEX"] // correctly returns 6

但是,这不会:
bigMoqVar.ComplexityWeights.ContainsKey("COMPLEX")  // returns false instead of true

在innerMoq 上支持ContainsKey 的推荐方法是什么?

最佳答案

那是因为您没有为 ContainsKey 设置期望值。您需要手动设置,Moq 不了解界面的语义。

innerMoq.Setup(d => d.ContainsKey("COMPLEX")).Returns(true);

但是,如果这只是您需要的 IDictionary,为什么要通过模拟框架?只需使用您需要的对象创建一个 Dictionary<string,double> 即可。

关于asp.net-mvc-3 - MOQ - 模拟一个 Dictionary<string, double> 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7337010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com