我创建了一个自定义功能过滤器,并在 EvaluateAsync 中从 Azure 的功能管理器获取此特定功能标志过滤器的功能标志的自定义设置。
public virtual Task<Boolean> EvaluateAsync(FeatureFilterEvaluationContext context)
{
_settings = context.Parameters.Get<IdFilterSettings>();
IdFilterSettings 如下所示:
internal class IdFilterSettings
{
public String[]? AllowedIds { get; set; }
public String[]? BlockedIds { get; set; }
}
当通过实际的 API 调用来调用它时,_settings
正确设置为 IdFilterSettings
的新实例与 AllowedIds
和BlockedIds
正确填充。
但是,当我尝试对此进行单元测试并模拟 FeatureFilterEvaluationContext
使用此代码:
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);
如果我在 _settings = context.Parameters.Get<IdFilterSettings>();
之后放置一个断点,settings 是 IdFilterSettings
的新实例,但是AllowedIds
和BlockedIds
均为空。
最佳答案
根据 jimmyca15 的回答 here ,我必须将参数 Dictionary 更改为:
var parameters = new Dictionary<string, string?>
{
{ "AllowedIds:0", "someId1" },
{ "AllowedIds:1", "someId2" },
{ "BlockedIds:0", "someId3" }
};
关于c# - 如何模拟FeatureFilterEvaluationContext参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76165742/