Mock IOptionsMonitor

丶灬走出姿态 提交于 2020-01-03 10:47:54

问题


How can I make an class instance manually of a class that requires an IOptionsMonitor in the constructor?

My Class

private readonly AuthenticationSettings _authenticationSettings;

public ActiveDirectoryLogic(IOptionsMonitor<AuthenticationSettings> authenticationSettings)
{            
   _authenticationSettings = authenticationSettings.CurrentValue;
}

My test

AuthenticationSettings au = new AuthenticationSettings(){ ... };
var someOptions = Options.Create(new AuthenticationSettings());
var optionMan = new OptionsMonitor(someOptions);  // dont work.           
ActiveDirectoryLogic _SUT = new ActiveDirectoryLogic(au);

I tried to make an IOptionsMonitor object manually but can't figure out how.


回答1:


You are calling the constructor of the OptionsMonitor<TOptions> class incorrectly.

In this case I would have just mocked the IOptionsMonitor<AuthenticationSettings> interface

For example using Moq

AuthenticationSettings au = new AuthenticationSettings() { ... };
var monitor = Mock.Of<IOptionsMonitor<AuthenticationSettings>>(_ => _.CurrentValue == au);
ActiveDirectoryLogic _SUT = new ActiveDirectoryLogic(monitor);


来源:https://stackoverflow.com/questions/55950932/mock-ioptionsmonitor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!