在 Moq 的同一成员设置上同时使用 When 和 Protected 方法
Using both When and Protected methods on the same member setup with Moq
有没有办法同时使用 Mock.Protected
和 Mock.When
进行单个成员设置,除了将 When
条件移动到设置回调中或在 if
块中调用常规 Setup
?
最小起订量的要求是保持任何现有设置不变,但在特定条件下仍然支持另一个设置。
我想做这样的事情:
mock.When(x => condition).Protected().Setup<int>("member").Returns(0);
目前无法同时使用这两个功能。
Mock<T>.When
定义为
public ISetupConditionResult<T> When(Func<bool> condition)`
版本=4.13.1.0
并且ISetupConditionResult<T>
不允许Proteced<T>
扩展方法
public static IProtectedMock<T> Protected<T>(this Mock<T> mock) where T : class;
因为它需要 Mock<T>
作为目标。
如果可能,使用正常条件,然后进行保护设置
if(condition) {
mock.Protected().Setup<int>("member").Returns(0);
}
有没有办法同时使用 Mock.Protected
和 Mock.When
进行单个成员设置,除了将 When
条件移动到设置回调中或在 if
块中调用常规 Setup
?
最小起订量的要求是保持任何现有设置不变,但在特定条件下仍然支持另一个设置。
我想做这样的事情:
mock.When(x => condition).Protected().Setup<int>("member").Returns(0);
目前无法同时使用这两个功能。
Mock<T>.When
定义为
public ISetupConditionResult<T> When(Func<bool> condition)`
版本=4.13.1.0
并且ISetupConditionResult<T>
不允许Proteced<T>
扩展方法
public static IProtectedMock<T> Protected<T>(this Mock<T> mock) where T : class;
因为它需要 Mock<T>
作为目标。
如果可能,使用正常条件,然后进行保护设置
if(condition) {
mock.Protected().Setup<int>("member").Returns(0);
}