.Net核心单元测试-模拟IOptions< T> [英] .Net Core Unit Testing - Mock IOptions<T>

查看:95
本文介绍了.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核心单元测试-模拟IOptions&lt; T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆