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

294

我觉得我很明显地漏掉了什么。 我有些类需要使用.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);
        }
    }
}

你是否对mocking的含义感到困惑?你可以在接口上进行mock,并配置它返回指定的值。对于IOptions<T>,你只需要mock Value以返回所需的类即可。 - Tseng
11个回答

550
你需要手动创建并填充一个IOptions<SampleOptions>对象。你可以通过Microsoft.Extensions.Options.Options帮助类来实现这一点。例如:
IOptions<SampleOptions> someOptions = Options.Create<SampleOptions>(new SampleOptions());

您可以将其简化为:

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

显然,这样使用起来并没有什么用。您需要实际创建并填充一个SampleOptions对象,并将其传递到Create方法中。


1
我很感激所有额外的答案,展示了如何使用Moq等。但这个答案非常简单,我肯定会使用它。而且它很棒! - grahamesd
1
很棒的答案。比依赖于模拟框架简单得多。 - Chris Lawrence
2
谢谢。我已经厌倦了在每个地方写new OptionsWrapper<SampleOptions>(new SampleOptions()); - BritishDeveloper
1
@BritishDeveloper 更好的方式是 var someOptions = Options.Create(new SampleOptions()); 吗? - Kiquenet
1
我正在使用 .Net 7,并且目前收到的消息是 类型名 'Create' 在类型 'Options' 中不存在。真是让人无语。 - Scott Fraley

99

如果你打算按照评论中@TSeng所示使用模拟框架,你需要在你的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);   

希望有所帮助。

顺便说一下,我在 Github/patvin80 上有一个git仓库,介绍了这两种方法。


真希望这对我有用,但它并没有 :( Moq 4.13.1 - kanpeki

41

您可以完全避免使用MOQ。在测试中使用.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);
    }
}

这对我很有效,谢谢!我不想为了看起来如此简单的事情而使用Moq,也不想尝试使用配置设置填充自己的选项。 - Harry
6
功能很好,但是关键缺失的信息是您需要包含Microsoft.Extensions.Configuration.Binder NuGet包,否则您将无法使用“Get<SomeServiceConfiguration>”扩展方法。 - Kinetic
我必须运行 dotnet add package Microsoft.Extensions.Configuration.Json 才能使其工作。非常好的答案! - Leonardo Wildt
1
我还需要更改appsettings.json文件的属性,以使用bin文件中的文件,因为Directory.GetCurrentDirectory()返回的是bin文件的内容。在appsettings.json的“复制到输出目录”中,我将值设置为“如果较新则复制”。 - bpz
使用MSTest单元测试吗? - Kiquenet

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

19

使用 Microsoft.Extensions.Options.Options 类:

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

或者

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

这个答案是对Necoras的高赞回答的一个很好的补充,因为它添加了花括号对象初始化。 - Theophilus

16

假设有一个依赖于PersonSettings的类Person,如下所示:

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

你并没有测试任何有用的东西。Microsoft 的 DI 框架已经进行了单元测试。就目前而言,这实际上是一个集成测试(与第三方框架集成)。 - Erik Philips
7
我的代码展示了如何按照OP的要求模拟IOptions<T>。我同意它本身并没有测试任何有用的东西,但是它可以在测试其他东西时变得有用。 - Frank Rem

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

以下是用于测试IOptions和IOptionsMonitor的示例代码:

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

要了解更多信息,请参加由Steve Gordon提供的.NET Core中使用配置和选项优秀课程,在该课程中他解释了如何测试IOptions。

enter image description here

  • MOQ和xunit

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here


谢谢!我一直想要一种简洁的方式来使用IOptions,但又不受其限制——因为这会让测试变得非常麻烦。 - codewise

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中随处使用配置。对于单元测试,我更喜欢使用MOQ,就像patvin80所描述的那样。

1
  1. 首先在根目录的unitTestProject中添加"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);

这似乎不对。嘲笑的整个重点就是不依赖于实际数据。 - basquiatraphaeu
  1. 只使用 Get<ClassName>() 而不是 Get();
  2. @basquiatraphaeu - 模拟数据可以与面向测试的 appsettings.json 结合使用,这样可以得到更准确的设置。我更倾向于并建议使用测试 appsettings.json 而不是模拟它。
- Ivan Silkin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接