c# - 将最小起订量与 Linq Any() 结合使用

标签 c# .net unit-testing mocking moq

我有一个类似于下面的设置:

    [TestMethod]
    public void NoIntegers()
    {
        Mock<IBar> mockBar = new Mock<IBar>(MockBehavior.Strict);
        Mock<IEnumerable<int>> mockIntegers = new Mock<IEnumerable<int>>(MockBehavior.Strict);

        mockBar
            .SetupGet(x => x.Integers)
            .Returns(mockIntegers.Object);

        mockIntegers
            .Setup(x => x.Any())
            .Returns(false);

        Assert.IsFalse(new Foo(mockBar.Object).AreThereIntegers());
    }

    public interface IBar
    {
        IEnumerable<int> Integers { get; }
    }

    public class Foo
    {
        private IBar _bar;

        public Foo(IBar bar)
        {
            _bar = bar;
        }

        public bool AreThereIntegers()
        {
            return _bar.Integers.Any();
        }
    }
}

运行时无法初始化模拟

Test method NoIntegers threw exception: System.NotSupportedException: Expression references a method that does not belong to the mocked object: x => x.Any<Int32>()

我尝试以几种形式添加 It.IsAny():

mockIntegers
    .Setup(x => x.Any(It.IsAny<IEnumerable<int>>(), It.IsAny<Func<int, bool>>()))
    .Returns(false);

// No method with this signiture


mockIntegers
    .Setup(x => x.Any(It.IsAny<Func<int, bool>>()))
    .Returns(false);

// Throws: Test method NoIntegers threw exception: 
// System.NotSupportedException: 
// Expression references a method that does not belong to the mocked object:
//  x => x.Any<Int32>(It.IsAny<Func`2>())

我需要模拟什么才能让它运行?

最佳答案

已修复!

不漂亮,但这是需要的模拟:

   [TestMethod]
    public void NoIntegers()
    {
        Mock<IBar> mockBar = new Mock<IBar>(MockBehavior.Strict);
        Mock<IEnumerable<int>> mockIntegers = new Mock<IEnumerable<int>>(MockBehavior.Strict);
        Mock<IEnumerator<int>> mockEnumerator = new Mock<IEnumerator<int>>(MockBehavior.Strict);
        mockBar
            .SetupGet(x => x.Integers)
            .Returns(mockIntegers.Object);

        mockIntegers
            .Setup(x => x.GetEnumerator())
            .Returns(mockEnumerator.Object);

        mockEnumerator.Setup(x => x.MoveNext()).Returns(false);

        mockEnumerator.Setup(x => x.Dispose());

        Assert.IsFalse(new Foo(mockBar.Object).AreThereIntegers());
    }

    public interface IBar
    {
        IEnumerable<int> Integers { get; }
    }

    public class Foo
    {
        private IBar _bar;

        public Foo(IBar bar)
        {
            _bar = bar;
        }

        public bool AreThereIntegers()
        {
            return _bar.Integers.Any();
        }
    }

关于c# - 将最小起订量与 Linq Any() 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20218887/

相关文章:

c# - WPF Lostfocus 无法正确触发 vs2010 treeviewitem

c# - 在非管理员帐户下运行自托管的 OWIN Web API

c# - 在此上下文中仅支持实体类型、枚举类型或原始类型

ruby-on-rails - 用于测试服务对象的 Rspec 建议

c# - 如何对调用 Response.AppendToLog 的方法进行单元测试?

c# - 捕获没有焦点的击键

c# - 如何使用 Linq to SQL 添加到列表?

.net - 如何使用 XSLT 删除属性值与给定值不同的节点?

.net - 来自多个项目的单个装配

Java:如何理解神秘的堆栈跟踪?