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

当前回答

你完全可以避免使用最小起订量。 在测试中使用.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);
    }
}

其他回答

下面是另一种不需要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);

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

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

你可以将其简化为:

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

显然这不是很有用。您需要实际创建和填充一个SampleOptions对象,并将其传递给create方法。

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

使用Microsoft.Extensions.Options.Options类:

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

OR

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

同意Aleha使用testSettings。Json配置文件可能更好。

然后,不是注入IOption<SampleOptions>,你可以简单地在你的类构造函数中注入真正的SampleOptions,当单元测试类时,你可以在一个fixture中或在测试类构造函数中执行以下操作:

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

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