Moq Index 属性 使用 SetupGet 回调
Moq Index property Callback with SetupGet
我正在尝试为扩展方法编写测试,该方法为发送到 IConfiguration 的索引 属性 的键添加前缀:
分机:
public static class IConfigurationExt
{
public static string GetDomainValue(this IConfiguration configuration, string key)
{
return configuration["domain." + key];
}
}
测试:
[Test]
public void GetInexKeyAsCallback()
{
string keySet = null;
Mock<IConfiguration> configurationMock = new Mock<IConfiguration>(MockBehavior.Strict);
configurationMock.SetupGet(p => p[It.IsAny<string>()])
.Callback(() => keySet = "assign key here") // <<< the part here needs the parameter
.Returns("mock");
IConfiguration configuration = configurationMock.Object;
var result = configuration.GetDomainValue("testKey");
Assert.AreEqual(expected: "domain.testKey", actual: keySet);
}
我正在尝试查看当执行 getter 并发送密钥时,它将带有 IConfiguration 索引 属性 的前缀。
我的问题是我无法使回调部分使用参数,例如:
.Callback<string>((key) => keySet = key)
,例如
有没有办法获取发送到索引 属性 的密钥?
它适用于 SetupSet
,但不适用于 SetupGet
谢谢!
SetupGet
没有允许访问传递参数的 Callback
。
改用Setup
,然后Callback
可以通过在委托中包含一个参数来访问传递的参数
public void GetInexKeyAsCallback() {
//Arrange
string actual = null;
string expected = "domain.testKey";
Mock<IConfiguration> configurationMock = new Mock<IConfiguration>(MockBehavior.Strict);
configurationMock
.Setup(_ => _[It.IsAny<string>()]) // <-- Use Setup
.Callback((string arg) => actual = arg) // <<< the part here gets the parameter
.Returns("mock");
IConfiguration configuration = configurationMock.Object;
//Act
var result = configuration.GetDomainValue("testKey");
//Assert
Assert.AreEqual(expected, actual);
}
从记忆你需要改变
Callback(() =>
至
Callback<string>( x =>
虽然您已经有了答案,但我想建议 IMO 更适合您所描述问题的另一种方法。
I am trying to see that when a getter is executed and a key is sent,
it will come with the prefix to the index property of IConfiguration.
Verify
用于这种情况而不是 Callback
。您可以将测试重写为如下内容:
// Arrange
const string expectedKey = "testKey";
Mock<IConfiguration> configurationMock = new Mock<IConfiguration>(MockBehavior.Strict);
configurationMock.SetupGet(p => p[It.IsAny<string>()]).Returns("mock");
// Act
_ = configurationMock.Object.GetDomainValue(expectedKey);
// Assert
configurationMock.Verify(m => m[$"domain.{expectedKey}"], Times.Once);
我正在尝试为扩展方法编写测试,该方法为发送到 IConfiguration 的索引 属性 的键添加前缀:
分机:
public static class IConfigurationExt
{
public static string GetDomainValue(this IConfiguration configuration, string key)
{
return configuration["domain." + key];
}
}
测试:
[Test]
public void GetInexKeyAsCallback()
{
string keySet = null;
Mock<IConfiguration> configurationMock = new Mock<IConfiguration>(MockBehavior.Strict);
configurationMock.SetupGet(p => p[It.IsAny<string>()])
.Callback(() => keySet = "assign key here") // <<< the part here needs the parameter
.Returns("mock");
IConfiguration configuration = configurationMock.Object;
var result = configuration.GetDomainValue("testKey");
Assert.AreEqual(expected: "domain.testKey", actual: keySet);
}
我正在尝试查看当执行 getter 并发送密钥时,它将带有 IConfiguration 索引 属性 的前缀。
我的问题是我无法使回调部分使用参数,例如:
.Callback<string>((key) => keySet = key)
,例如
有没有办法获取发送到索引 属性 的密钥?
它适用于 SetupSet
,但不适用于 SetupGet
谢谢!
SetupGet
没有允许访问传递参数的 Callback
。
改用Setup
,然后Callback
可以通过在委托中包含一个参数来访问传递的参数
public void GetInexKeyAsCallback() {
//Arrange
string actual = null;
string expected = "domain.testKey";
Mock<IConfiguration> configurationMock = new Mock<IConfiguration>(MockBehavior.Strict);
configurationMock
.Setup(_ => _[It.IsAny<string>()]) // <-- Use Setup
.Callback((string arg) => actual = arg) // <<< the part here gets the parameter
.Returns("mock");
IConfiguration configuration = configurationMock.Object;
//Act
var result = configuration.GetDomainValue("testKey");
//Assert
Assert.AreEqual(expected, actual);
}
从记忆你需要改变
Callback(() =>
至
Callback<string>( x =>
虽然您已经有了答案,但我想建议 IMO 更适合您所描述问题的另一种方法。
I am trying to see that when a getter is executed and a key is sent, it will come with the prefix to the index property of IConfiguration.
Verify
用于这种情况而不是 Callback
。您可以将测试重写为如下内容:
// Arrange
const string expectedKey = "testKey";
Mock<IConfiguration> configurationMock = new Mock<IConfiguration>(MockBehavior.Strict);
configurationMock.SetupGet(p => p[It.IsAny<string>()]).Returns("mock");
// Act
_ = configurationMock.Object.GetDomainValue(expectedKey);
// Assert
configurationMock.Verify(m => m[$"domain.{expectedKey}"], Times.Once);