繁体   English   中英

使用Moq模拟GetEnumerator

[英]Mocking GetEnumerator using Moq

我试图在Microsoft.Office.Interop.Word程序集中模拟Variables接口

var variables = new Mock<Variables>();
variables.Setup(x => x.Count).Returns(2);
variables.Setup(x => x.GetEnumerator()).Returns(TagCollection);

private IEnumerator TagCollection()
{
    var tag1 = new Mock<Variable>();
    tag1.Setup(x => x.Name).Returns("Foo");
    tag1.Setup(x => x.Value).Returns("Bar");

    var tag2 = new Mock<Variable>();
    tag2.Setup(x => x.Name).Returns("Baz");
    tag2.Setup(x => x.Value).Returns("Qux");

    yield return tag1.Object;
    yield return tag2.Object;
}

我的代码如下所示:

// _variables is an instance of Variables interface
var tags = from variable in _variables.OfType<Variable>()
           where variable.Name == "Foo"
           select variable.Value;
var result = tags.ToList();

上面代码中的最后一行抛出NullReferenceException。 如果我使用foreach循环迭代_variables集合,我可以毫无问题地访问Variable的模拟对象。 我在这做错了什么?

尝试:

variables
    .As<IEnumerable>()
    .Setup(x => x.GetEnumerator()).Returns(TagCollection);

有两种不同的方法,一种在基本接口中声明,另一种在Variables声明。

直接进行foreach ,会调用后者,因为该方法会隐藏基类型中相同的成员。 foreach在存在时调用public方法,在这种情况下, IEnumerable无关紧要。

当您调用.OfType<Variable>() Linq扩展时,引用将转换为IEnumerable接口,并且名称隐藏不再存在。 调用基接口上的方法。

这就像之间的区别:

_variables.GetEnumerator();

和:

((IEnumerable)_variables).GetEnumerator();

您可以将模拟Moq视为:

public class TheTypeMoqMakes : Variables 
{
  Enumerator Variables.GetEnumerator()
  {
    // Use return value from after
    // expression tree you provided with 'Setup' without 'As'.
    // If you did not provide one, just return null.
  }

  Enumerator IEnumerable.GetEnumerator()
  {
    // Use return value from after
    // expression tree you provided with 'Setup' with 'As<IEnumerable>'.
    // If you did not provide one, just return null.
  }

  // other methods and properties
}

如果成员不是Setup ,Moq返回null的原因是你有MockBehavior.Loose 总是考虑使用MockBehavior.Strict


我无法理解为什么Variables接口的作者在这种情况下选择使用方法隐藏

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM