Moq设置:
Branch3GInfo mockCasResponse = new Branch3GInfo
{
Branch3GActiveRouters = new CasApiRouterModelExtendedInfo[]
{
new CasApiRouterModelExtendedInfo(12345, "MyRouter", 4, 1, "MyModelType")
}
};
var mock = new Mock<BranchCircuitStatusScheduleEntry>(_application);
mock.CallBase = true;
mock.Setup(x => x.GetNodesOn3GBackupAsync()).ReturnsAsync(mockCasResponse);
var mockedObject = mock.Object.GetNodesOn3GBackupAsync().Result;
mockedObject包含我创建的模拟响应。
BranchCircuitStatusScheduleEntry有一个名为GetNodesInCriticalCondition()的方法。
GetNodesInCriticalCondition()在其体内调用GetNodesOn3GBackupAsync(),然后根据它生成一些输出。但是,它进行了这个调用,它调用REAL GetNodesOn3GBackupAsync(),而不是我的模拟版本。有没有办法让这个工作?
答案 0 :(得分:0)
您应该创建BranchCircuitStatusScheduleEntry的新实例,然后调用GetNodesInCriticalCondition,在该方法内部,您将获得GetNodesOn3GBackupAsync的模拟结果。
var branchCircuitStatusScheduleEntry = new BranchCircuitStatusScheduleEntry();
var result = branchCircuitStatusScheduleEntry.GetNodesInCriticalCondition();