.NET Core 2.0 单元测试中初识 IOptionsMonitor<T>
2017-08-30 07:21
459 查看
在针对下面设置 CookieAuthenticationOptions 的扩展方法写单元测试时遇到了问题。
想通过下面的单元测试验证对 CookieAuthenticationOptions 的设置是否生效:
但通过依赖注入解析 IOptions<CookieAuthenticationOptions> 接口得到的 CookieAuthenticationOptions 实例的值都是默认值, AddCnblogsAuthentication() 中的设置没生效。
后来查看 CookieAuthenticationHandler 的实现代码才知道需要通过 IOptionsMonitor<CookieAuthenticationOptions> 接口解析,而且需要调用该接口的 Get() 方法(而不是 CurrentValue 属性)根据指定的 AuthenticationScheme 才能获取到所需的 CookieAuthenticationOptions 实例。
原文地址:http://www.cnblogs.com/dudu/p/7424667.html
.NET社区新闻,深度好文,微信中搜索dotNET跨平台或扫描二维码关注
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 单元测试中初识 IOptionsMonitor<T>
- <Android>初识一:令人头疼的R文件缺失问题
- <html:select>,<html:option>,<html:options>,<html:optionsCollection>标签使用
- pass all options of <select> from View to controller
- 【002】初识C++ <二 > 保留关键字(Typedef)、构造器和析构器
- <一>Ioc控制反转——手把手教学:如何配置并使用单元测试一个spring框架(附源码)
- AFNetworking2.0源码解析<二>
- MVC2.0本地化(另类解决方案)&“.NET研究”lt;下>
- Android实战简易教程<九>(BitmapFactory.Options对资源图片进行缩放)
- Note On <Programming Entity Framework 2nd Edition> -02: How to Use SqlExpress Profiler 2.0
- <C#入门经典>学习笔记1之初识C#
- 用struts 2.0标签 遍历 List<List<String>> listCollection
- struts标签格式化时间<s:date name="neowaveBloodPressure.monitorTime" format="yyyy-MM-dd HH:mm:ss" />
- 玩转Google开源C++单元测试框架Google Test系列(gtest)(总) &lt;转&gt;
- <菜鸟必看>初识for增强循环
- AFNetworking2.0源码解析<一>
- AFNetworking2.0源码解析<一>
- <10> go test单元测试
- AFNetworking2.0源码解析<一>
- 一起学Maven(Maven仓库管理器之初识Nexus)<五>