【问题标题】:.NET Core Unit Testing - Mock IOptions<T>.NET Core 单元测试 - 模拟 IOptions<T>
【发布时间】:2017-04-14 01:49:54
【问题描述】:

我觉得我在这里遗漏了一些非常明显的东西。我有需要使用 .NET Core IOptions 模式(?)注入选项的类。当我对该类进行单元测试时,我想模拟各种版本的选项来验证该类的功能。有谁知道如何在 Startup 类之外正确地模拟/实例化/填充IOptions&lt;T&gt;

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

设置/选项模型

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 的意思?您模拟一个接口并将其配置为返回指定的值。对于IOptions&lt;T&gt;,您只需模拟Value 即可返回您想要的课程

标签: c# unit-testing configuration asp.net-core


【解决方案1】:

使用 Microsoft.Extensions.Options.Options 类:

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

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

【讨论】:

    【解决方案2】:

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

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

    var builder = new ConfigurationBuilder()
        .AddJsonFile("testSettings.json", true, true)
        .AddEnvironmentVariables();
    
    var configurationRoot = builder.Build();
    configurationRoot.GetSection("SampleRepo").Bind(_sampleRepo);
    

    【讨论】:

      【解决方案3】:

      您始终可以通过 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; }
      }
      

      【讨论】:

        【解决方案4】:

        给定类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&lt;PersonSettings&gt; 可以模拟,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&lt;PersonSettings&gt; 注入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");
            }
        }
        

        【讨论】:

        • 你没有测试任何有用的东西。 DI my Microsoft 的框架已经过单元测试。就目前而言,这实际上是一个集成测试(与第 3 方框架集成)。
        • @ErikPhilips 我的代码显示了如何按照 OP 的要求模拟 IOptions。我同意它本身不会测试任何有用的东西,但它可以用于测试其他东西。
        【解决方案5】:

        这是另一个不需要 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);
        

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            【解决方案7】:

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

            【讨论】:

            • 这对我很有效,干杯!不想将起订量用于看起来如此简单的事情,也不想尝试使用配置设置填充我自己的选项。
            • 效果很好,但重要的缺失信息是您需要包含 Microsoft.Extensions.Configuration.Binder nuget 包,否则您不会获得“Get”扩展方法可用。
            • 我必须运行 dotnet add package Microsoft.Extensions.Configuration.Json 才能让它工作。很好的答案!
            • 我还必须更改 appsettings.json 文件的属性以使用 bin 文件中的文件,因为 Directory.GetCurrentDirectory() 正在返回 bin 文件的内容。在 appsettings.json 的“复制到输出目录”中,我将值设置为“如果较新则复制”。
            【解决方案8】:

            如果您打算使用注释中@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 上概述了这两种方法

            【讨论】:

            • 这应该是公认的答案,效果很好。
            • 真的希望这对我有用,但它没有:( Moq 4.13.1
            • @alessandrocb 接受的答案也是如此。
            【解决方案9】:

            您需要手动创建和填充IOptions&lt;SampleOptions&gt; 对象。您可以通过 Microsoft.Extensions.Options.Options 辅助类来实现。例如:

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

            您可以将其简化为:

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

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

            【讨论】:

            • 我感谢所有显示如何使用 Moq 等的其他答案,但这个答案非常简单,绝对是我正在使用的答案。而且效果很好!
            • 很好的答案。比依赖模拟框架要简单得多。
            • 谢谢。我已经厌倦了到处写new OptionsWrapper&lt;SampleOptions&gt;(new SampleOptions());
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-06-07
            • 1970-01-01
            • 1970-01-01
            • 2018-10-06
            相关资源
            最近更新 更多