Reputation: 17548
I have a .NET core MVC rest service. I have a controller I wish to test. This controller has a constructor argument of IOptions where AppSettings is my class for config settings ( I store my database connection string in it). It gets injected from the setup in ConfigureServices in Startup.cs
The rest service works. My problem is I've set up a MSTest test project to test the service. I can't figure out how to initialize an instance of IOptions to satisfy the constructor of my controller.
Upvotes: 66
Views: 28904
Reputation: 17548
I discovered the answer shortly after posting the question.
use Helper class Microsoft.Extensions.Options.Options
Creates a wrapper around an instance of TOptions to return itself as IOptions
AppSettings appSettings = new AppSettings() { ConnectionString = "..." };
IOptions<AppSettings> options = Options.Create(appSettings);
MyController controller = new MyController(options);
Upvotes: 122