使用Moq模拟FusionCache时遭遇CS1929编译错误的解决方案求助
大家好,我最近给FusionCache写了个包装类,现在想给它写单元测试,但是遇到了个编译错误,搞了半天没头绪,想请教下有没有大佬遇到过类似问题?
先贴下我的FusionCache包装类代码:
public class FusionCacheService<TEntity> : IFusionCacheService<TEntity> where TEntity : class { private readonly IFusionCache _cache; private readonly PayabliMainDbContext _dbContext; private readonly ILogger<FusionCacheService<TEntity>> _logger; private readonly int _cacheDuration; public FusionCacheService( IFusionCache cache, PayabliMainDbContext dbContext, ILogger<FusionCacheService<TEntity>> logger, IConfiguration configuration) { _cache = cache; _dbContext = dbContext; _logger = logger; _cacheDuration = Convert.ToInt32(configuration["CacheDuration"] ?? "5"); } // Get all values from the cache public async Task<List<TEntity>> GetAllAsync<T>(string key, CancellationToken cancellationToken) { try { return await _cache.GetOrSetAsync( key, async _ => await _dbContext.Set<TEntity>().ToListAsync(cancellationToken), TimeSpan.FromMinutes(_cacheDuration), cancellationToken ); } catch (Exception ex) { _logger.LogError(ex, "Error getting cache for key: {Key}", key); return new List<TEntity>(); } } }
然后Copilot帮我生成了下面的单元测试,看起来逻辑是通的,但编译时就报错了:
var options = new DbContextOptionsBuilder<PayabliMainDbContext>() .UseInMemoryDatabase(databaseName: "TestDatabase") .Options; _dbContext = new PayabliMainDbContext(options); // Seed the in-memory database _dbContext.Set<TestEntity>().AddRange(new List<TestEntity> { new TestEntity { Id = 1, Name = "Test Entity 1" }, new TestEntity { Id = 2, Name = "Test Entity 2" } }); _dbContext.SaveChanges(); // Mock configuration for cache duration _mockConfiguration.Setup(c => c["CacheDuration"]).Returns("5"); } [Fact] public async Task GetAllAsync_ShouldReturnCachedData_WhenCacheExists() { // Arrange var cacheKey = "test-key"; var cachedData = new List<TestEntity> { new TestEntity { Id = 1, Name = "Cached Entity 1" } }; _mockCache .Setup(c => c.GetOrSetAsync( It.IsAny<string>(), It.IsAny<Func<FusionCacheEntryOptions, Task<List<TestEntity>>>>(), It.IsAny<TimeSpan>(), It.IsAny<CancellationToken>())) .ReturnsAsync(cachedData); var service = new FusionCacheService<TestEntity>( _mockCache.Object, _dbContext, _mockLogger.Object, _mockConfiguration.Object); // Act var result = await service.GetAllAsync<TestEntity>(cacheKey, CancellationToken.None); // Assert Assert.NotNull(result); Assert.Single(result); Assert.Equal("Cached Entity 1", result.First().Name); }
编译时Visual Studio报了这个错误:
CS1929 'ISetup<IFusionCache, ValueTask<Func<FusionCacheEntryOptions, Task
>>>' does not contain a definition for 'ReturnsAsync' and the best extension method overload 'SequenceExtensions.ReturnsAsync
(ISetupSequentialResult<Task
>, List)' requires a receiver of type 'Moq.Language.ISetupSequentialResult<System.Threading.Tasks.Task<System.Collections.Generic.List<Payabli.Data.PostgreSql.Main.Cache.Tests.TestEntity>>>
我查了下Moq的文档,明明是有ReturnsAsync方法的啊,而且这个错误提示的类型看起来也不对,感觉是不是泛型匹配出问题了?有没有大佬之前模拟过FusionCache的GetOrSetAsync方法,能给我指个方向吗?
内容来源于stack exchange

