gpt4 book ai didi

c# - 压缩代码 : SetupSequence returning different values on successive calls

转载 作者:行者123 更新时间:2023-11-30 20:34:25 24 4
gpt4 key购买 nike

我正在测试调用其他方法的方法。我有一个使用此方法生成模拟连接对象的工作测试:

    private Mock<IDatabaseConnection> MockOutGetControlDocInfoData()
{
Mock<IDatabaseConnection> mockConn = new Mock<IDatabaseConnection>();
List<Mock<IDbCommand>> mockCmds = new List<Mock<IDbCommand>>();
List<long> vals = new List<long>() { 2, 2, 2, 2, 10, 2, 2, 2, 2, 2 };
foreach (long val in vals)
{
mockCmds.Add(CreateMockCmdObjectWithReturnValue(val));
}
mockConn.SetupAllProperties();
mockConn.Setup(c => c.Conn.ConnectionString).Returns("What the heck.");
mockConn.SetupSequence(c => c.CreateCommand(It.IsAny<string>()))
.Returns(mockCmds[0].Object)
.Returns(mockCmds[1].Object)
.Returns(mockCmds[2].Object)
.Returns(mockCmds[3].Object)
.Returns(mockCmds[4].Object)
.Returns(mockCmds[5].Object)
.Returns(mockCmds[6].Object)
.Returns(mockCmds[7].Object)
.Returns(mockCmds[8].Object)
.Returns(mockCmds[9].Object);
return mockConn;
}

我对 SetupSequence Returns 不满意,它看起来应该是循环的一部分,但我不知道如何将多个 Returns 放入 SetupSequence。有什么改进的想法吗?

至少现在,我可以手动创建测试值列表。

它应该不相关,但如果需要,我可以提供 CreateMockCmdObjectWithReturnValue 代码。

最佳答案

var sequence = mockConn.SetupSequence(c => c.CreateCommand(It.IsAny<string>()));
foreach (var cmd in mockCmds)
{
sequence = sequence.Returns(cmd.Object);
}

关于c# - 压缩代码 : SetupSequence returning different values on successive calls,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39259637/

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