【问题标题】:Unit Test and IDistributedCache using azure redis使用 azure redis 进行单元测试和 IDistributedCache
【发布时间】:2018-09-14 15:57:10
【问题描述】:

我有一个 Azure Redis 和 .Net Core 2 的工作实现,使用的代码与 article 中描述的代码非常相似

我的问题是,如何从单元测试类中实例化缓存实例?我浏览了许多资源,但一无所获。

我需要能够创建一个实例来实例化一个类,例如

    public CacheManager(IDataManager dataservices, IDistributedCache cache)
    {
        _cache = cache;
        _dataservices = dataservices;
    }

startup.cs 中的代码使用了 ConfigureServices

            //Configure Redis Cache
        var redisconnection = Configuration.GetConnectionString("Redis");
        services.AddDistributedRedisCache(o => { o.Configuration = redisconnection; });

也许我需要在单元测试项目中添加一个包?这是怎么做到的?

【问题讨论】:

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


    【解决方案1】:

    我使用Microsoft.Extensions.Caching.Distributed.MemoryDistributedCache 类进行单元测试。这是IDistributedCache 的内存实现。

    这是我的单元测试代码的 sn-p。

            [TestMethod]
            public void ExampleTestMethod()
            {
                var expectedData = new byte[] { 100, 200 };
    
                var opts = Options.Create<MemoryDistributedCacheOptions>(new MemoryDistributedCacheOptions());
                IDistributedCache cache1 = new MemoryDistributedCache(opts);
                cache1.Set("key1", expectedData);
                var cachedData = cache1.Get("key1");
    
                Assert.AreEqual(expectedData, cachedData);
    
               //Use the variable cache as an input to any class which expects IDistributedCache
            }
    
    

    在我的示例中,我使用的是 .NET Core 3.1,相关的 NUGET 包是

        <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.2.0" />
        <PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="3.1.4" />
    
    

    【讨论】:

      【解决方案2】:

      您可以模拟接口以根据独立单元测试的需要运行。

      public void Test_CacheManager() {
          //Arrange
          IDataManager dataservices = new Mock<IDataManager>(); 
          IDistributedCache cache = new Mock<IDistributedCache>();
          var subject = new CacheManager(dataservices.Object, cache.Object);
      
          //Setup the mocks to behave as expected.
      
          //Act
          //...call the method under test
      
          //Assert
          //...assert the expected behavior
      }
      

      上面的示例使用 Moq 来演示如何模拟被测类的依赖项的实例。

      参考Moq Quickstart 以更好地了解如何使用模拟库。

      如果您要连接到实际的 redis 连接,那么这将不再是单元测试,而是集成测试,这需要完全不同的方法。

      public void Test_CacheManager() {
          //Arrange
      
          IDataManager dataservices = new Mock<IDataManager>(); 
           //Setup the mocks to behave as expected.
      
          //Configure Redis Cache
          var services = new ServiceCollection();
          var redisconnection = "...";
          services.AddDistributedRedisCache(o => { o.Configuration = redisconnection; });
          var provider = services.BuildServiceProvider();
          IDistributedCache cache = provider.GetService<IDistributedCache>();
      
          var subject = new CacheManager(dataservices.Object, cache);
      
          //Act
          //...call the method under test
      
          //Assert
          //...assert the expected behavior
      }
      

      【讨论】:

      • 有没有办法创建可以访问 Azure Redis 的实际实现?
      • @user2503078 这将需要检查源代码以了解他们是如何做到的,然后尝试复制它。否则,您将需要走完全不同主题的集成测试路线。
      • 我的错,我指的是许多人所说的集成测试。答案的第二部分是我找不到的代码。谢谢
      • 这个扩展在哪里? services.AddDistributedRedisCache
      • @user2503078 您可能缺少对关联库的引用以及测试文件顶部的 using。检查docs.microsoft.com/en-us/dotnet/api/…
      【解决方案3】:

      这就是我在 .net core 2.0 和 3.1 中设法做到这一点的方式。 您需要使用 MemoryDistributedCache。并通过您的服务构造函数传递它。实际上,您的服务通过依赖注入使用 IDistributedCache。 MemoryDistributedCache 也实现了 IDistributedCache。

      var mockedCacheSettings = new Mock<IOptions<CacheSettings>>();
      
              var options = new OptionsWrapper<MemoryDistributedCacheOptions>(new MemoryDistributedCacheOptions());
              _memoryDistributedCache = new MemoryDistributedCache(options);
              _distributedCacheService = new DistributedCacheService(_memoryDistributedCache, mockedCacheSettings.Object, NullLogger<DistributedCacheService>.Instance);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-06
        相关资源
        最近更新 更多