.NET Core 单元测试 - 模拟 IOptions<T>

问题描述 投票:0回答:12

我觉得我在这里遗漏了一些非常明显的东西。我有一些类需要使用 .NET Core

IOptions
模式(?)注入选项。当我对该类进行单元测试时,我想模拟选项的各种版本以验证该类的功能。有谁知道如何在启动类之外正确模拟/实例化/填充
IOptions<T>

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

设置/选项模型

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; }
    }
}

要测试的类使用设置:

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()
        {
        }
    }
}

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

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);
        }
    }
}
c# unit-testing configuration asp.net-core
12个回答
550
投票

您需要手动创建并填充一个

IOptions<SampleOptions>
对象。您可以通过
Microsoft.Extensions.Options.Options
辅助类来完成此操作。例如:

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

您可以将其简化为:

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

显然这并不是很有用。您需要实际创建并填充 SampleOptions 对象并将其传递到 Create 方法中。


99
投票

如果您打算使用评论中@TSeng指出的Mocking Framework,则需要在project.json文件中添加以下依赖项。

   "Moq": "4.6.38-alpha",

一旦恢复依赖关系,使用 MOQ 框架就像创建 SampleOptions 类的实例一样简单,然后如上所述将其分配给 Value。

这是一个代码概述,它的外观。

SampleOptions app = new SampleOptions(){Title="New Website Title Mocked"}; // Sample property
// Make sure you include using Moq;
var mock = new Mock<IOptions<SampleOptions>>();
// We need to set the Value of IOptions to be the SampleOptions Class
mock.Setup(ap => ap.Value).Returns(app);

设置模拟后,您现在可以将模拟对象传递给构造函数

SampleRepo sr = new SampleRepo(mock.Object);   

HTH。

仅供参考,我有一个 git 存储库,它在 Github/patvin80

上概述了这两种方法

41
投票

您完全可以避免使用最小起订量。 在测试 .json 配置文件中使用。一个文件可容纳多个测试类文件。在这种情况下使用

ConfigurationBuilder
就可以了。

appsetting.json 示例

{
    "someService" {
        "someProp": "someValue
    }
}

设置映射类示例:

public class SomeServiceConfiguration
{
     public string SomeProp { get; set; }
}

需要测试的服务示例:

public class SomeService
{
    public SomeService(IOptions<SomeServiceConfiguration> config)
    {
        _config = config ?? throw new ArgumentNullException(nameof(_config));
    }
}

NUnit 测试类:

[TestFixture]
public class SomeServiceTests
{

    private IOptions<SomeServiceConfiguration> _config;
    private SomeService _service;

    [OneTimeSetUp]
    public void GlobalPrepare()
    {
         var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", false)
            .Build();

        _config = Options.Create(configuration.GetSection("someService").Get<SomeServiceConfiguration>());
    }

    [SetUp]
    public void PerTestPrepare()
    {
        _service = new SomeService(_config);
    }
}

28
投票

您始终可以通过 Options.Create() 创建选项,而不是在实际创建您正在测试的存储库的模拟实例之前简单地使用 AutoMocker.Use(options) 。使用 AutoMocker.CreateInstance<>() 可以更轻松地创建实例,无需手动传递参数

我对 SampleRepo 进行了一些更改,以便能够重现我认为您想要实现的行为。

public class SampleRepoTests
{
    private readonly AutoMocker _mocker = new AutoMocker();
    private readonly ISampleRepo _sampleRepo;

    private readonly IOptions<SampleOptions> _options = Options.Create(new SampleOptions()
        {FirstSetting = "firstSetting"});

    public SampleRepoTests()
    {
        _mocker.Use(_options);
        _sampleRepo = _mocker.CreateInstance<SampleRepo>();
    }

    [Fact]
    public void Test_Options_Injected()
    {
        var firstSetting = _sampleRepo.GetFirstSetting();
        Assert.True(firstSetting == "firstSetting");
    }
}

public class SampleRepo : ISampleRepo
{
    private SampleOptions _options;

    public SampleRepo(IOptions<SampleOptions> options)
    {
        _options = options.Value;
    }

    public string GetFirstSetting()
    {
        return _options.FirstSetting;
    }
}

public interface ISampleRepo
{
    string GetFirstSetting();
}

public class SampleOptions
{
    public string FirstSetting { get; set; }
}

20
投票

使用 Microsoft.Extensions.Options.Options 类:

var someOptions= Options.Create(new SampleOptions(){Field1="Value1",Field2="Value2"});

var someOptions= Options.Create(new SampleOptions{Field1="Value1",Field2="Value2"});

16
投票

给定类

Person
取决于
PersonSettings
,如下所示:

public class PersonSettings
{
    public string Name;
}

public class Person
{
    PersonSettings _settings;

    public Person(IOptions<PersonSettings> settings)
    {
        _settings = settings.Value;
    }

    public string Name => _settings.Name;
}

IOptions<PersonSettings>
可以被模拟,
Person
可以测试如下:

[TestFixture]
public class Test
{
    ServiceProvider _provider;

    [OneTimeSetUp]
    public void Setup()
    {
        var services = new ServiceCollection();
        // mock PersonSettings
        services.AddTransient<IOptions<PersonSettings>>(
            provider => Options.Create<PersonSettings>(new PersonSettings
            {
                Name = "Matt"
            }));
        _provider = services.BuildServiceProvider();
    }

    [Test]
    public void TestName()
    {
        IOptions<PersonSettings> options = _provider.GetService<IOptions<PersonSettings>>();
        Assert.IsNotNull(options, "options could not be created");

        Person person = new Person(options);
        Assert.IsTrue(person.Name == "Matt", "person is not Matt");    
    }
}

要将

IOptions<PersonSettings>
注入
Person
而不是将其显式传递给 ctor,请使用以下代码:

[TestFixture]
public class Test
{
    ServiceProvider _provider;

    [OneTimeSetUp]
    public void Setup()
    {
        var services = new ServiceCollection();
        services.AddTransient<IOptions<PersonSettings>>(
            provider => Options.Create<PersonSettings>(new PersonSettings
            {
                Name = "Matt"
            }));
        services.AddTransient<Person>();
        _provider = services.BuildServiceProvider();
    }

    [Test]
    public void TestName()
    {
        Person person = _provider.GetService<Person>();
        Assert.IsNotNull(person, "person could not be created");

        Assert.IsTrue(person.Name == "Matt", "person is not Matt");
    }
}

11
投票

这是另一种简单的方法,不需要 Mock,而是使用 OptionsWrapper:

var myAppSettingsOptions = new MyAppSettingsOptions();
appSettingsOptions.MyObjects = new MyObject[]{new MyObject(){MyProp1 = "one", MyProp2 = "two", }};
var optionsWrapper = new OptionsWrapper<MyAppSettingsOptions>(myAppSettingsOptions );
var myClassToTest = new MyClassToTest(optionsWrapper);

2
投票

对于我的系统和集成测试,我更喜欢在测试项目中拥有配置文件的副本/链接。然后我使用 ConfigurationBuilder 来获取选项。

using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace SomeProject.Test
{
public static class TestEnvironment
{
    private static object configLock = new object();

    public static ServiceProvider ServiceProvider { get; private set; }
    public static T GetOption<T>()
    {
        lock (configLock)
        {
            if (ServiceProvider != null) return (T)ServiceProvider.GetServices(typeof(T)).First();

            var builder = new ConfigurationBuilder()
                .AddJsonFile("config/appsettings.json", optional: false, reloadOnChange: true)
                .AddEnvironmentVariables();
            var configuration = builder.Build();
            var services = new ServiceCollection();
            services.AddOptions();

            services.Configure<ProductOptions>(configuration.GetSection("Products"));
            services.Configure<MonitoringOptions>(configuration.GetSection("Monitoring"));
            services.Configure<WcfServiceOptions>(configuration.GetSection("Services"));
            ServiceProvider = services.BuildServiceProvider();
            return (T)ServiceProvider.GetServices(typeof(T)).First();
        }
    }
}
}

这样我就可以在 TestProject 中的任何地方使用该配置。对于单元测试,我更喜欢使用像 patvin80 所描述的最小起订量。


2
投票

这里是使用测试 IOptions 和 IOptionsMonitor 的示例代码

  • 接口(将 IOptions 转换为 IConfigurationClass,然后使用接口进行测试)

要了解更多信息,Steve Gordon 提供了关于 在 .NET Core 中使用配置和选项 的精彩课程,其中解释了如何测试 IOptions

  • 最小起订量和xunit


1
投票
  1. 首先在根单元TestProject中添加“appsettings.json”文件

  2. 然后使用此代码:

    private readonly Mock _fileRepMock;
    private IOptions _options;
    public FileServiceTest()
    {
       _fileRepMock = new Mock();
        var config = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
             .AddEnvironmentVariables()
             .Build();
        _options = Options.Create(config.GetSection("File").Get());
    }
  3. 现在您可以在模拟存储库中使用_options

    FileService fileService = new FileService(_fileRepMock.Object, _options);

0
投票

同意 Aleha 的观点,使用 testSettings.json 配置文件可能更好。

然后,您可以简单地在类构造函数中注入真正的 SampleOptions,而不是注入 IOption,当对类进行单元测试时,您可以在固定装置中执行以下操作,或者在测试类构造函数中再次执行以下操作:

var builder = new ConfigurationBuilder()
    .AddJsonFile("testSettings.json", true, true)
    .AddEnvironmentVariables();

var configurationRoot = builder.Build();
configurationRoot.GetSection("SampleRepo").Bind(_sampleRepo);

0
投票

老话题,但想与静态帮助器类分享这种方式

// Configure default options or specified ones
public static void Configure(this Mock<IOptions<MyOptions>> mock, MyOptions options = null)
    => mock.Setup(x => x.Value).Returns(options ?? new MyOptions());

// You can define your common configurations in this class
static readonly MyOptions Config2 = new()
    {
        FirstSetting = "value2"
    };

测试课

// Create tested class with options mock
Mock<IOptions<MyOptions>> options = new();

var myTestedClass = new MyTestClass(options.Object)

// Init or arrange test with default option object
options.Configure();

// Arrange test with a common option object 
options.Configure(Config2);

// Arrange test with specified option object
options.Configure(new MyOptions()
{
   FirstSetting = "value3"
});

如第一条评论中所述:使用 new() 然后设置你的模拟 IOptions.Value

© www.soinside.com 2019 - 2024. All rights reserved.