I pass IOption<T>
to my CommandBus
so I can get the settings from my ServiceBusSetting
class. I want to do an integration test of my Bus. I do not want to resolve it just use new QueueCommandBus
and need to pass IOptions
to it.
var services = new ServiceCollection().AddOptions();
services.Configure<ServiceBusAppSettings>(Configuration.GetSection("ServiceBus"));
var options = services.BuildServiceProvider().GetService<IOptions<ServiceBusAppSettings>>();
////Act
var commandBus = new QueueCommandBus(options);
This works fine, but feels very complex code to get the IOptions<T>
from my appsetting.json
in my test project.
Any clue if this is the only way or is there a better way?
You don't need to create the
ServiceCollection
orIServiceProvider
. TheIConfiguration
interface has aBind()
method, or from .NET Core 1.1 onwards,Get<T>
which you can use to get the strongly-typed object directly:I like to add these as static methods to my
AppSettings
strongly-typed object, to make it convenient to load them from JSON in both my web app and from unit tests.AppSettings.cs:
ASP.NET Core
Startup.cs
: (Getting the strongly-typed settings object is often helpful at this stage, when configuring the other services...)In Test Class: