gpt4 book ai didi

c# - Moq - 不能模拟类属性的方法返回值

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

如何模拟 CasOperations.GetImpairedNodesFromCASpectrumAsync() 方法,以便它返回模拟值?目前,我遇到异常(显示在代码示例下方)。

我有以下设置:

要模拟的类和属性:

public class BranchCircuitStatusScheduleEntry : NWatchCustomScheduleEntryBase, INWatchCustomScheduleEntry
{
public BranchCircuitStatusScheduleEntry(INWatchSchedulerApplication application)
: base(application, DevOpsScheduleFrequency.Minute, 15, 0, DevOpsScheduleFlags.Always)
{
// Some initialization for below properties
CasOperations = new CasOperations(cas, EntityService, IsBranchesOnly);
}

public CasOperations CasOperations { get; private set; }

}

public class CasOperations
{
public CasOperations(CasApi casApi, BranchCircuitEntityService entityService, bool isBranchesOnly)
{
CAS = casApi;
this.entityService = entityService;
this.isBranchesOnly = isBranchesOnly;
}
}

尝试执行模拟的测试:

[TestMethod]
public void DownNodeRediscoveredInSpectrum()
{
var mock = new Mock<BranchCircuitStatusScheduleEntry>(_application);
mock.CallBase = true;

// Spectrum's artificial response with a model with the same name, but a "new" model handle
var mockedNewlyImpairedNodes = new NetworkDeviceNodeStatus[]
{
new NetworkDeviceNodeStatus
{
// Not important
}
};

mock.Setup(x =>
x.CasOperations.GetImpairedNodesFromCASpectrumAsync()).ReturnsAsync(mockedNewlyImpairedNodes);
}

测试抛出的异常:

An exception of type 'Castle.DynamicProxy.InvalidProxyConstructorArgumentsException' occurred in Moq.dll but was not handled in user code Additional information: Can not instantiate proxy of class: NWatch.NetworkCircuits.CasOperations. Could not find a parameterless constructor.

最佳答案

让我试一试:

在模拟设置中,您并不是要模拟 BranchCircuitStatusScheduleEntry 的行为,但是 CasOperations 的行为类(class)。所以你真的需要一个 Mock<CasOperations>目的。或者更好的是 Mock<ICasOperations>正如@Jonesopolis 在他的评论中所说。

此外,在 BranchCircuitStatusScheduleEntry构造函数您正在初始化该类的实例和 CasOperations 的实例.如果你初始化 CasOperations 的实例会更好外面BranchCircuitStatusScheduleEntry构造函数并将其作为参数传递。

所以它会是这样的:

public class BranchCircuitStatusScheduleEntry : NWatchCustomScheduleEntryBase, INWatchCustomScheduleEntry
{
public BranchCircuitStatusScheduleEntry(INWatchSchedulerApplication application, ICasOperations casOperations)
: base(application, DevOpsScheduleFrequency.Minute, 15, 0, DevOpsScheduleFlags.Always)
{
CasOperations = casoperations;
}

public CasOperations CasOperations { get; private set; }
}

最后,您创建模拟,设置它并将其作为参数传递给 BranchCircuitStatusScheduleEntry构造函数:

var casMock = new Mock<ICasOperations>();
casMock.Setup(x => x.GetImpairedNodesFromCASpectrumAsync()).ReturnsAsync(mockedNewlyImpairedNodes);

var mock = new Mock<BranchCircuitStatusScheduleEntry>(_application, casMock.Object);
mock.CallBase = true;

请注意,这可能是 BranchCircuitStatusScheduleEntry 的最后一个实例不应是模拟对象,而应是真实对象(被测实例)。

关于c# - Moq - 不能模拟类属性的方法返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37400345/

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