简体   繁体   English

从 IReadOnlyCollection 继承的模拟类

[英]Mock class which inherits from IReadOnlyCollection

I would like to mock a class which inherits from IReadOnlyCollection.我想模拟一个继承自 IReadOnlyCollection 的类。 I have written some example code to demonstrate my issue.我编写了一些示例代码来演示我的问题。 Two out of the Three Asserts work.三个断言中的两个有效。

When I cast mocked IRemainingSteps to a list or use LINQ the list is empty.当我将模拟的 IRemainingSteps 转换为列表或使用 LINQ 时,列表为空。

Please, can you explain how I should change the setup of the GetEnumerator to allow all three Asserts to pass.请您解释一下我应该如何更改 GetEnumerator 的设置以允许所有三个断言通过。

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Moq;
using NUnit.Framework;

namespace BranchScript.UT
{
    [TestFixture]
    public class Steps
    {
        [Test]
        public void Test()
        {
            // Arrange
            string branchA = "branch a";
            string branchB = "branch b";

            var mockStep1 = new Mock<IRemainingStep>();
            mockStep1.Setup(x => x.StepNotes).Returns(branchA);

            var mockStep2 = new Mock<IRemainingStep>();
            mockStep2.Setup(x => x.StepNotes).Returns(branchB);

            var mockStep3 = new Mock<IRemainingStep>();
            mockStep3.Setup(x => x.StepNotes).Returns(branchA);

            var mockStep4 = new Mock<IRemainingStep>();
            mockStep4.Setup(x => x.StepNotes).Returns(branchB);

            List<IRemainingStep> mockStepList = new List<IRemainingStep>
            {
                mockStep1.Object,
                mockStep2.Object,
                mockStep3.Object,
                mockStep4.Object
            };

            var refs = new Mock<IRemainingSteps>(MockBehavior.Strict);
            refs.Setup(r => r.GetEnumerator()).Returns(mockStepList.GetEnumerator());
            refs.As<IEnumerable>().Setup(r => r.GetEnumerator()).Returns(mockStepList.GetEnumerator());

            var mockPlate = new Mock<IPlate>();
            mockPlate.Setup(x => x.RemainingSteps).Returns(refs.Object);

            // Assert
            Assert.AreEqual(branchA, mockPlate.Object.RemainingSteps.First().StepNotes); // Pass
            Assert.AreEqual(branchB, mockPlate.Object.RemainingSteps.Last().StepNotes); // Pass
            Assert.AreEqual(2, mockPlate.Object.RemainingSteps.Where(x => x.StepNotes == branchA).Count()); // Fail
        }
    }

    public interface IRemainingSteps : IReadOnlyCollection<IRemainingStep>
    {
    }

    public interface IRemainingStep : IStep
    {
    }

    public interface IStep
    {
        string StepNotes { get; }
    }

    public interface IPlate
    {
        IRemainingSteps RemainingSteps { get; }
    }
}

Passing back the Enumerator only allow one read, because it is forward only.传回 Enumerator 只允许一次读取,因为它是仅向前的。

var refs = new Mock<IRemainingSteps>(MockBehavior.Strict);
refs.Setup(r => r.GetEnumerator()).Returns(mockStepList.GetEnumerator());
refs.As<IEnumerable>().Setup(r => r.GetEnumerator()).Returns(mockStepList.GetEnumerator());

But if the mock returns a function it will allow for repeated calls to the enumerator但是如果模拟返回一个函数,它将允许重复调用枚举器

var refs = new Mock<IRemainingSteps>(MockBehavior.Strict);
refs.Setup(r => r.GetEnumerator()).Returns(() => mockStepList.GetEnumerator());
refs.As<IEnumerable>().Setup(r => r.GetEnumerator()).Returns(() => mockStepList.GetEnumerator());

Note the use of the function call in the Returns注意Returns函数调用的使用

.Returns(() => mockStepList.GetEnumerator())

The first two assertion work as you are still moving forward in the enumerator.前两个断言有效,因为您仍在枚举器中前进。

Assert.AreEqual(branchA, mockPlate.Object.RemainingSteps.First().StepNotes); // Pass
Assert.AreEqual(branchB, mockPlate.Object.RemainingSteps.Last().StepNotes); // Pass

By the third assertion the pointer is already at the end通过第三个断言,指针已经在末尾

Assert.AreEqual(2, mockPlate.Object.RemainingSteps.Where(x => x.StepNotes == branchA).Count()); // Fail

so the count will not be as expected.所以计数不会像预期的那样。

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

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