gpt4 book ai didi

c# - 继承自 IReadOnlyCollection 的 Mock 类

转载 作者:行者123 更新时间:2023-11-30 22:53:28 26 4
gpt4 key购买 nike

我想模拟一个继承自 IReadOnlyCollection 的类。我写了一些示例代码来演示我的问题。三项断言中有两项有效。

当我将模拟的 IRemainingSteps 转换为列表或使用 LINQ 时,列表为空。

请解释一下我应该如何更改 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; }
}
}

最佳答案

传回 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());

但是如果模拟返回一个函数,它将允许重复调用枚举器

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());

注意 Returns 中函数调用的使用

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

当您仍在枚举器中前进时,前两个断言起作用。

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

所以计数不会像预期的那样。

关于c# - 继承自 IReadOnlyCollection 的 Mock 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57173297/

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