I can't figure out how to get some configuration I'm using by following the IOptions pattern.
The service I want to test is the next one:
private readonly Dictionary<ErrorType, ErrorObject> _errors;
public UserService(
...(Many services)...
IOptions<Dictionary<ErrorType, ErrorObject>> errors)
{
...
_errors = errors.Value;
}
Where ErrorType is an Enum and ErrorObject, an object with the format of the errors I have in my appsettings.json. This is it:
{
"Errors": {
"UserNotFound": {
"ErrorCode": 101,
"Message": "User not found"
},
"WrongPassword": {
"ErrorCode": 102,
"Message": "Wrong password"
}
}
}
So this works perfectly fine, but my problem now is I don't know how to instantiate it, mock it, or inject it in my test class, since even I'm injecting it I can't resolve it in such class and I always get a null. I injected it like this in the test fixture (same as in my real injection module):
serviceCollection.Configure<Dictionary<BusinessErrorType, BusinessErrorObject>>(x => Configuration.GetSection("Errors").Bind(x));
I got to properly inject the IConfiguration with all the errors, I just can't manage to create the required Dictionary for the constructor.
Here I get the errors without any problem:
IConfiguration Configuration = ioCModule.ServiceProvider.GetRequiredService<IConfiguration>();
var errorsConfig = Configuration.GetSection("Errors");
And now I gotta figure out what to put in this declaration:
var _options = Options.Create<Dictionary<BusinessErrorType, BusinessErrorObject>>();