Comment puis-je créer manuellement une instance de classe d'une classe qui nécessite un IOptionsMonitor dans le constructeur?
Ma classe
AuthenticationSettings au = new AuthenticationSettings(){ ... };
var someOptions = Options.Create(new AuthenticationSettings());
var optionMan = new OptionsMonitor(someOptions); // dont work.
ActiveDirectoryLogic _SUT = new ActiveDirectoryLogic(au);
Mon test
private readonly AuthenticationSettings _authenticationSettings;
public ActiveDirectoryLogic(IOptionsMonitor<AuthenticationSettings> authenticationSettings)
{
_authenticationSettings = authenticationSettings.CurrentValue;
}
J'ai essayé de créer un objet IOptionsMonitor manuellement mais je ne peux pas comprendre comment.
3 Réponses :
Vous appelez le constructeur de la OptionsMonitor<TOptions> de manière incorrecte.
Dans ce cas, je me serais juste moqué de l' IOptionsMonitor<AuthenticationSettings>
Par exemple en utilisant Moq
AuthenticationSettings au = new AuthenticationSettings() { ... };
var monitor = Mock.Of<IOptionsMonitor<AuthenticationSettings>>(_ => _.CurrentValue == au);
ActiveDirectoryLogic _SUT = new ActiveDirectoryLogic(monitor);
Voici une autre façon de le faire qui n'implique pas d'essayer de définir le champ CurrentValue en lecture seule.
using Moq;
private IOptionsMonitor<AppConfig> GetOptionsMonitor(AppConfig appConfig)
{
var optionsMonitorMock = new Mock<IOptionsMonitor<AppConfig>>();
optionsMonitorMock.Setup(o => o.CurrentValue).Returns(appConfig);
return optionsMonitorMock.Object;
}
Obtenir la même chose dans NSubstitute:
var optionsMonitorMock = Substitute.For<IOptionsMonitor<AuthenticationSettings>>();
optionsMonitorMock.CurrentValue.Returns(new AuthenticationSettings
{
// values go here
});
docs.microsoft.com/en-us/dotnet/api/…
Dans ce cas, je me serais juste moqué de l'interface