I am trying to write a test for an extension method that adds a prefix for a key that gets sent to the index property of IConfiguration:
extension:
public static class IConfigurationExt
{
public static string GetDomainValue(this IConfiguration configuration, string key)
{
return configuration["domain." + key];
}
}
test:
[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);
}
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.
My problem is that I cannot make the Callback part working with a parameter , such as:
.Callback<string>((key) => keySet = key), for example.
Is there a way of getting the key that was sent to the indexed property?
It works with SetupSet, but not with SetupGet
Thanks!