在针对下面设置 CookieAuthenticationOptions 的扩展方法写单元测试时遇到了问题。

public static IServiceCollection AddCnblogsAuthentication(this IServiceCollection services, IConfigurationSection redisConfiguration, Action<CookieAuthenticationOptions> configureOption = null)
{    //...}

想通过下面的单元测试验证对 CookieAuthenticationOptions 的设置是否生效:

public void AddCnblogsAuthenticationTest()
{IServiceCollection services = new ServiceCollection();    var builder = new ConfigurationBuilder();builder.AddInMemoryCollection(new Dictionary<string, string>{["redis"] = JsonConvert.SerializeObject(new CnblogsRedisOptions())});    var configuration = builder.Build();services.AddCnblogsAuthentication(configuration.GetSection("redis"),option =>{option.LoginPath = "/users/signin";});    var options = services.BuildServiceProvider().GetRequiredService<IOptions<CookieAuthenticationOptions>>().Value;Assert.Equal("/users/signin", options?.LoginPath);
}

但通过依赖注入解析 IOptions<CookieAuthenticationOptions> 接口得到的 CookieAuthenticationOptions 实例的值都是默认值, AddCnblogsAuthentication() 中的设置没生效。

后来查看 CookieAuthenticationHandler 的实现代码才知道需要通过 IOptionsMonitor<CookieAuthenticationOptions> 接口解析,而且需要调用该接口的 Get() 方法(而不是 CurrentValue 属性)根据指定的 AuthenticationScheme 才能获取到所需的 CookieAuthenticationOptions 实例。

public void AddCnblogsAuthenticationTest()
{    //...var options = services.BuildServiceProvider().GetRequiredService<IOptionsMonitor<CookieAuthenticationOptions>>().Get(CookieAuthenticationDefaults.AuthenticationScheme);Assert.Equal("/users/signin", options?.LoginPath);
}

原文地址:http://www.cnblogs.com/dudu/p/7424667.html


.NET社区新闻,深度好文,微信中搜索dotNET跨平台或扫描二维码关注

.NET Core 2.0 单元测试中初识 IOptionsMonitoramp;lt;Tamp;gt;相关推荐

  1. 使用xUnit为.net core程序进行单元测试(中)

    第一部分:  使用xUnit为.net core程序进行单元测试(上), 下面有一点点内容是重叠的.... String Assert 测试string是否相等: [Fact] public void ...

  2. NET Core 3.0 项目中使用 AutoFac

    .net core 3.1 今天已正式发布,3.1跟3.0差别不是很大,主要是对 3.0一小部分修复和完善,最重要的是.NET Core 3.1是长期支持(LTS)版本,建议大家升级. .net co ...

  3. IHostingEnvironment VS IHostEnvironment - .NET Core 3.0中的废弃类型

    原文:https://andrewlock.net/ihostingenvironment-vs-ihost-environment-obsolete-types-in-net-core-3/ 作者: ...

  4. 从ASP.NET Core 3.0 preview 特性,了解CLR的Garbage Collection

    前言 在阅读这篇文章:Announcing Net Core 3 Preview3的时候,我看到了这样一个特性: Docker and cgroup memory Limits We conclude ...

  5. Entity Framework 6.3 和 EF Core 3.0 路线图

    尽管脱离了 .NET Core 发布循环,但是 EF Core 正在开发其 3.0 路线图.除此之外,还对原来的 Entity Framework 进行了一些重要的变更. 更多服务器端的查询 将 LI ...

  6. EntityFramework Core 2.0执行原始查询如何防止SQL注入?

    前言 接下来一段时间我们来讲讲EntityFramework Core基础,精简的内容,深入浅出,希望为想学习EntityFramework Core的童鞋提供一点帮助. EntityFramewor ...

  7. .NET Core 1.0.1 发布了

    今天微软发布了 .NET Core 1.0.1,这是 .NET Core 1.0 的首个微软长期支持更新,同时发布的还有 ASP.NET Core 1.0.1 与 Entity Framework C ...

  8. java对接华为推送服务_华为HMS Core 4.0版本即将来袭:包含新推送服务

    据官方消息,在12月27日的HUAWEI Developer Groups(简称HDG)大连活动上,华为消费者云服务HMS运营经理透露HMS Core 4.0版本即将上线,由原来的14项增加至24项核 ...

  9. .NET Core 3.0 正式公布:新特性详细解读

    点击蓝字"dotNET匠人"关注我哟 加个"星标★",每日 7:15,好文必达! 作者丨Richard Lander 译者丨核子可乐 策划丨赵钰莹 近日,.NE ...

最新文章

  1. 实战分享:淘宝Web 3D应用与游戏开发
  2. 解决JQuery.Treeview在CI中无法加载查询函数的方法
  3. libtorch 调用pytorch模型
  4. day22 模块-collections,time,random,pickle,shelve等
  5. vue 固定div 滚动_vue.js-div滚动条隐藏但有滚动效果的实现方法
  6. ASCII码排序(C++)
  7. boost库在ubuntu下的安装
  8. Spring Boot和Vue的学习(一)--开发环境的安装
  9. python locust_python locust--性能测试框架从零开始(三)
  10. 人工智能作业——搜索树博弈树一阶逻辑表达式CNF范式
  11. destoon php文件,Destoon模板制作简明教程
  12. CSS实现DIV悬浮
  13. Java实现二分查找算法(非递归)
  14. 安装LuaRocks
  15. php selectradio和checkbox默认选择的实现方法
  16. MATLAB不能打字,电脑不能打字怎么办?电脑打字打不出来解决方法汇总
  17. MySql两台服务器(主机)间热备份(双机热备份)
  18. 基于Spring Boot实现电脑端网页微信扫码授权登录方式一(附带完整源码)
  19. vue加载图片失败处理
  20. [吐槽] 微软的DreamSpark

热门文章

  1. 10分钟学会vue滚动行为
  2. 安装分布式文件系统MooseFS
  3. Oracle行列转换的思考与总结
  4. C++学习之路: 线程封装(基于对象编程)
  5. Android STL PORT
  6. oracle 11g ORA-12541: TNS: 无监听程序 (DBD ERROR: OCIServerAttach)
  7. 充分利用系统的组策略 保障共享目录安全
  8. WPF 实现动态Windows桌面壁纸~
  9. dotnet 通过引用 msbuild 程序集实现自己定制编译器
  10. .NET 搭建简单的通知服务