Moq - 模拟方法响应未返回预期值

时间:2016-03-25 22:00:59

标签: c# unit-testing moq

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()而不是我的模拟版本。有没有办法让这个工作?

1 个答案:

答案 0 :(得分:0)

您应该创建BranchCircuitStatusScheduleEntry的新实例,然后调用GetNodesInCriticalCondition,在该方法内部,您将获得GetNodesOn3GBackupAsync的模拟结果。

var branchCircuitStatusScheduleEntry  = new BranchCircuitStatusScheduleEntry();
var result = branchCircuitStatusScheduleEntry.GetNodesInCriticalCondition();