简体   繁体   English

如何最小化Linq扩展方法

[英]How to moq Linq extension methods

I'm trying to moq an object in which it will be requerable in my test class. 我正在尝试在我的测试班级中对它进行重新请求的对象。 I couldn't do it because apperently moq dosen't moq static methods. 我无法做到这一点,因为显然没有足够的静态方法。

    var mockConfiguration = new Mock<IConfiguration>();
    mockConfiguration.Setup(f => f.AsEnumerable()
                                      .Where(kvp => kvp.Key.StartsWith($"CheckerConfigurations:{nameof(OSVersionChecker)}:SupportedVersions:") && kvp.Value != null)
                                      .Select(kvp => kvp.Value)).Returns(new List<string>() {
                                          "Android 5",
                                          "Android 6",
                                          "Darwin Kernel Version 15",
                                          "Darwin Kernel Version 16",
                                          "Windows 10",
                                          "Windows 6.2",
                                          "Windows 6.3"
                                      }.AsEnumerable());

Can anyone show me how to do it please ? 谁能告诉我该怎么做?

You can't mock static methods and so all LINQ methods are not (easy) mockable (in fact it is possible, due to ordering of namespaces and which extension method will be picked), but that is not what you like to do. 您不能模拟静态方法,因此所有LINQ方法都不是(简单的)可模拟的(事实上,由于命名空间的排序和将选择哪种扩展方法,这是可能的),但这不是您想要做的。

Instead you should create an instance (regardless if mocked or real) which provides the desired data and inject this into the methods you like to test. 相反,您应该创建一个实例(无论是模拟的还是真实的),该实例提供所需的数据并将其注入您要测试的方法中。

In case of IConfiguration there is even no mock needed. IConfiguration情况下,甚至不需要模拟。 You can use an existing implementation that provides the desired data. 您可以使用提供所需数据的现有实现。

So for your case i would use the following one: 因此,对于您的情况,我将使用以下内容:

[Fact]
public void ConfigurationContainsSupportedVersions()
{
    var config = CreateConfiguration();
    var items = config.GetSection($"CheckerConfigurations:{nameof(OSVersionChecker)}:SupportedVersions").Get<List<string>>();

    Assert.Equal(7, items.Count);
}

private static IConfiguration CreateConfiguration()
{
    return new ConfigurationBuilder()
        .AddInMemoryCollection(new Dictionary<string, string>
        {
        { $"CheckerConfigurations:{nameof(OSVersionChecker)}:SupportedVersions:0", "Android 5" },
        { $"CheckerConfigurations:{nameof(OSVersionChecker)}:SupportedVersions:1", "Android 6" },
        { $"CheckerConfigurations:{nameof(OSVersionChecker)}:SupportedVersions:2", "Darwin Kernel Version 15" },
        { $"CheckerConfigurations:{nameof(OSVersionChecker)}:SupportedVersions:3", "Darwin Kernel Version 16" },
        { $"CheckerConfigurations:{nameof(OSVersionChecker)}:SupportedVersions:4", "Windows 10" },
        { $"CheckerConfigurations:{nameof(OSVersionChecker)}:SupportedVersions:5", "Windows 6.2" },
        { $"CheckerConfigurations:{nameof(OSVersionChecker)}:SupportedVersions:6", "Windows 6.3" },
        })
        .Build();
}

This config instance can be forwarded to any method that consumes an IConfiguration and it produces the desired output you like. 可以将此config实例转发到使用IConfiguration的任何方法,并产生所需的所需输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM