LINQ to Mocks:访问方法参数
LINQ to Mocks: accessing method parameters
刚接触 Moq 库时,我决定使用 LINQ 来设置模拟。我看过很多示例,这些示例解释了在使用设置模拟的 "traditional" 方法时如何使用 .Returns 访问方法的参数值。
但是如何使用 LINQ to Mocks 做到这一点?我的代码在下面,它没有 运行,因为我找不到命名 val
的方法。 val
应该是第二个参数的值。 (UpdatedValue
是单元测试的局部变量。)
IAppSettingLogic MoqAsl = Mock.Of<IAppSettingLogic>(asl =>
asl.SetAppSettingAsync(AppSettings.LatestReconciliationValue, It.IsAny<string>(), It.IsAny<string>()) ==
Task.Run(() => UpdatedValue = val));
对于您所描述的复杂场景,通常使用传统格式,因为 linq to mock 更适合简单的快速设置。 Linq to mock 不允许访问参数值
var UpdateValue = string.Empty;
var mock = new Mock<IAppSettingLogic>();
mock
.Setup(_ => _.SetAppSettingAsync(AppSettings.LatestReconciliationValue, It.IsAny<string>(), It.IsAny<string>())
.Returns( (string arg0, string val, string args3) => {
UpdatedValue = val;
return Task.CompletedTask;
});
IAppSettingLogic MoqAsl = mock.Object;
LINQ to Mocks is great for quickly stubbing out dependencies that typically don't need further verification. If you do need to verify later some invocation on those mocks, you can easily retrieve them with Mock.Get(instance)
如果您有可以快速模拟的简单成员,您可以混合使用这两种格式
//Ling to Mock
IAppSettingLogic MoqAsl = Mock.Of<IAppSettingLogic>(asl => asl.SimpleMember == someSimpleValue);
//Add traditional setup to get access to return function using "Mock.Get(T)"
Mock.Get(MoqAsl)
.Setup(_ => _.SetAppSettingAsync(AppSettings.LatestReconciliationValue, It.IsAny<string>(), It.IsAny<string>())
.Returns( (string arg0, string val, string args3) => {
UpdatedValue = val;
return Task.CompletedTask;
});
刚接触 Moq 库时,我决定使用 LINQ 来设置模拟。我看过很多示例,这些示例解释了在使用设置模拟的 "traditional" 方法时如何使用 .Returns 访问方法的参数值。
但是如何使用 LINQ to Mocks 做到这一点?我的代码在下面,它没有 运行,因为我找不到命名 val
的方法。 val
应该是第二个参数的值。 (UpdatedValue
是单元测试的局部变量。)
IAppSettingLogic MoqAsl = Mock.Of<IAppSettingLogic>(asl =>
asl.SetAppSettingAsync(AppSettings.LatestReconciliationValue, It.IsAny<string>(), It.IsAny<string>()) ==
Task.Run(() => UpdatedValue = val));
对于您所描述的复杂场景,通常使用传统格式,因为 linq to mock 更适合简单的快速设置。 Linq to mock 不允许访问参数值
var UpdateValue = string.Empty;
var mock = new Mock<IAppSettingLogic>();
mock
.Setup(_ => _.SetAppSettingAsync(AppSettings.LatestReconciliationValue, It.IsAny<string>(), It.IsAny<string>())
.Returns( (string arg0, string val, string args3) => {
UpdatedValue = val;
return Task.CompletedTask;
});
IAppSettingLogic MoqAsl = mock.Object;
LINQ to Mocks is great for quickly stubbing out dependencies that typically don't need further verification. If you do need to verify later some invocation on those mocks, you can easily retrieve them with
Mock.Get(instance)
如果您有可以快速模拟的简单成员,您可以混合使用这两种格式
//Ling to Mock
IAppSettingLogic MoqAsl = Mock.Of<IAppSettingLogic>(asl => asl.SimpleMember == someSimpleValue);
//Add traditional setup to get access to return function using "Mock.Get(T)"
Mock.Get(MoqAsl)
.Setup(_ => _.SetAppSettingAsync(AppSettings.LatestReconciliationValue, It.IsAny<string>(), It.IsAny<string>())
.Returns( (string arg0, string val, string args3) => {
UpdatedValue = val;
return Task.CompletedTask;
});