I'm using IOptions<>
according to https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options.
I have the following controller.
using Microsoft.Extensions.Options;
public class MyController : Controller
{
private readonly ILogger<SourceController> _logger;
private readonly MyOptions _option;
public SourceController(ILogger<SourceController> logger, IOptions<SyncOptions> optionAccessor)
{
_logger = logger;
_option = optionAccessor.Value;
}
Now I'm creating a unit test setup. How to mock IOptions()?
MyTest() { // Test constructor
var loggerFactory = new LoggerFactory().AddConsole(LogLevel.Information).AddDebug().AddNLog();
loggerFactory.ConfigureNLog("nlog.config");
logger = loggerFactory.CreateLogger<MyController>();
option = new Options<MyOptions>(); // Error! How to mock IOptions<MyOptions>()?
app = new MyController(logger, option);
Use helper class Microsoft.Extensions.Options.Options
Try replacing
option = new Options<MyOptions>();
with
option = Options.Create(new MyOptions());
What about:
var optionsMock = new Mock<IOptions<MyOptions>>();
optionsMock.SetupGet(o => o.Value).Returns(new MyOptions());
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