c# - 继承自 IReadOnlyCollection 的模拟类
问题描述
我想模拟一个从 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
所以计数不会像预期的那样。
推荐阅读
- node.js - Node-soap - 无法解码 Gzip 编码的响应
- vscode-extensions - vs 代码扩展开发。地狱无法运行
- php - 有人想破解吗?在我的 Apache2 Ubuntu 18.04 服务器上接收可疑请求
- python - How can I convert 1 ½ to 1.5
- windows - USRP X310 无法通过 1GbE 识别
- c++ - 有没有办法将 const class* 转换为非 const 类
- php - 用星号播放来电者广播流
- r - openxlsx Excel公式连续,如何为每列动态创建公式
- c# - 如何使用 C# 从 MS Project Rest api 读取纯文本?
- debian-jessie - Debian 10 上的 pdf2htmlEX 与 Drupal 一起使用