我觉得我忽略了一些很明显的东西。我有一些类需要使用. 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);
        }
    }
}

当前回答

你总是可以通过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; }
}

其他回答

对于我的系统和集成测试,我更喜欢在测试项目中有配置文件的副本/链接。然后我使用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所描述的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);
    }
}

first add "appsettings.json" file in the root unitTestProject then use this code: 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()); } now you can use _options in the mock repository FileService fileService = new FileService(_fileRepMock.Object, _options);

如果你打算使用@TSeng在评论中指出的mock框架,你需要在你的项目中添加以下依赖项。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);

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

SampleRepo sr = new SampleRepo(mock.Object);   

HTH。

仅供参考,我在Github/patvin80上有一个git存储库,概述了这2种方法

你总是可以通过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; }
}