.Net核心单元测试

编程入门 行业动态 更新时间:2024-10-11 15:18:51
本文介绍了.Net核心单元测试-模拟IOptions< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我觉得我在这里确实缺少一些明显的东西.我有一些类需要使用.Net Core IOptions pattern(?)注入选项.当我对该类进行单元测试时,我想模拟各种版本的选项以验证该类的功能.有谁知道如何在Startup类之外正确模拟/实例化/填充IOptions?

I feel like I'm missing something really obvious here. I have classes that require injecting of options using the .Net Core IOptions pattern(?). When I go to unit test that class I want to mock various versions ofo the options to validate the functionality of the class. Does anyone know how to correctly mock/instantiate/populate IOptions outside of the Startup class?

以下是我正在使用的课程的一些示例:

Here are some samples of the classes I'm working with:

设置/选项模型

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace OptionsSample.Models { public class SampleOptions { public string FirstSetting { get; set; } public int SecondSetting { get; set; } } }

使用设置"的要测试的类:

Class to be tested which uses the Settings:

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using OptionsSample.Models using System.Net.Http; using Microsoft.Extensions.Options; using System.IO; using Microsoft.AspNetCore.Http; using System.Xml.Linq; using Newtonsoft.Json; using System.Dynamic; using Microsoft.Extensions.Logging; namespace OptionsSample.Repositories { public class SampleRepo : ISampleRepo { private SampleOptions _options; private ILogger<AzureStorageQueuePassthru> _logger; public SampleRepo(IOptions<SampleOptions> options) { _options = options.Value; } public async Task Get() { } } }

在与其他类不同的程序集中进行单元测试:

Unit test in a different assembly from the other classes:

using OptionsSample.Repositories; using OptionsSample.Models; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Xunit; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Configuration; namespace OptionsSample.Repositories.Tests { public class SampleRepoTests { private IOptions<SampleOptions> _options; private SampleRepo _sampleRepo; public SampleRepoTests() { //Not sure how to populate IOptions<SampleOptions> here _options = options; _sampleRepo = new SampleRepo(_options); } } }

推荐答案

您需要手动创建并填充IOptions<SampleOptions>对象.您可以通过Microsoft.Extensions.Options.Options辅助类来实现.例如:

You need to manually create and populate an IOptions<SampleOptions> object. You can do so via the Microsoft.Extensions.Options.Options helper class. For example:

IOptions<SampleOptions> someOptions = Options.Create<SampleOptions>(new SampleOptions());

您可以将其简化为:

var someOptions = Options.Create(new SampleOptions());

很明显,这不是很有用.您实际上需要创建并填充SampleOptions对象,然后将其传递给Create方法.

Obviously this isn't very useful as is. You'll need to actually create and populate a SampleOptions object and pass that into the Create method.

更多推荐

.Net核心单元测试

本文发布于:2023-11-13 23:01:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585559.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   核心   Net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!