Reputation: 8270
I created a custom feature filter and in EvaluateAsync I get the feature flag's custom settings from Azure's Feature manager for this specific feature flag's filter.
public virtual Task<Boolean> EvaluateAsync(FeatureFilterEvaluationContext context)
{
_settings = context.Parameters.Get<IdFilterSettings>();
Where IdFilterSettings looks like this:
internal class IdFilterSettings
{
public String[]? AllowedIds { get; set; }
public String[]? BlockedIds { get; set; }
}
When calling it through an actual API call, _settings
properly gets set to a new instance of IdFilterSettings
with AllowedIds
and BlockedIds
properly populated.
But, when I try to unit test this and mock FeatureFilterEvaluationContext
with this code:
var allowedIds = new string[2] { "someId1", "someId2" };
var blockedIds = new string[1] { "someId3" };
var parameters = new Dictionary<string, string?>
{
{ "AllowedIds", JsonSerializer.Serialize(allowedIds) },
{ "BlockedIds", JsonSerializer.Serialize(blockedIds) }
};
var context = new FeatureFilterEvaluationContext
{
Parameters = new ConfigurationBuilder().AddInMemoryCollection(parameters).Build()
};
_filter = new CustomIdFilter(_httpContextAccessor.Object);
var result = await _filter.EvaluateAsync(context);
If I put a break point after _settings = context.Parameters.Get<IdFilterSettings>();
, settings is a new instance of IdFilterSettings
, but AllowedIds
and BlockedIds
are both null.
Upvotes: 2
Views: 256
Reputation: 8270
As per jimmyca15's answer here, I had to alter the parameters Dictionary to be:
var parameters = new Dictionary<string, string?>
{
{ "AllowedIds:0", "someId1" },
{ "AllowedIds:1", "someId2" },
{ "BlockedIds:0", "someId3" }
};
Upvotes: 2