For the normal IOptions interface, you can manually build an instance e.g. this SO question.
Is there any equivalent way to make an IOptionsMonitor instance without using DI?
IOptionsMonitor is a Singleton service that retrieves current option values at any time, which is especially useful in singleton dependencies.
You need to create a class with MyOptions[] as a property and the inject that class and add the whole configuration without section.
nice answer from Hananiel
Here the generic version of it:
public class TestOptionsMonitor<T> : IOptionsMonitor<T>
where T : class, new()
{
public TestOptionsMonitor(T currentValue)
{
CurrentValue = currentValue;
}
public T Get(string name)
{
return CurrentValue;
}
public IDisposable OnChange(Action<T, string> listener)
{
throw new NotImplementedException();
}
public T CurrentValue { get; }
}
and simply create an instance with your object!
You can do something like below and and then use that for testing:
public class TestOptionsMonitor : IOptionsMonitor<MyOptions>
{
public TestOptionsMonitor(MyOptions currentValue)
{
CurrentValue = currentValue;
}
public MyOptions Get(string name)
{
return CurrentValue;
}
public IDisposable OnChange(Action<MyOptions, string> listener)
{
throw new NotImplementedException();
}
public MyOptions CurrentValue { get; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With