- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用 asp net core 1.0 和 xunit。
我正在尝试为一些使用 IMemoryCache
的代码编写单元测试。但是,每当我尝试在 IMemoryCache
中设置一个值时,我都会收到 Null 引用错误。
我的单元测试代码是这样的:IMemoryCache
被注入(inject)到我要测试的类中。但是,当我尝试在测试中的缓存中设置一个值时,我得到了一个空引用。
public Test GetSystemUnderTest()
{
var mockCache = new Mock<IMemoryCache>();
return new Test(mockCache.Object);
}
[Fact]
public void TestCache()
{
var sut = GetSystemUnderTest();
sut.SetCache("key", "value"); //NULL Reference thrown here
}
这是类测试...
public class Test
{
private readonly IMemoryCache _memoryCache;
public Test(IMemoryCache memoryCache)
{
_memoryCache = memoryCache;
}
public void SetCache(string key, string value)
{
_memoryCache.Set(key, value, new MemoryCacheEntryOptions {SlidingExpiration = TimeSpan.FromHours(1)});
}
}
我的问题是...我是否需要以某种方式设置 IMemoryCache
?为 DefaultValue 设置一个值?当 IMemoryCache
被 Mocked 时,默认值是什么?
最佳答案
IMemoryCache.Set
是一种扩展方法,因此不能使用 Moq 进行模拟框架。
扩展代码可用 here
public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, MemoryCacheEntryOptions options)
{
using (var entry = cache.CreateEntry(key))
{
if (options != null)
{
entry.SetOptions(options);
}
entry.Value = value;
}
return value;
}
对于测试,需要通过扩展方法模拟一条安全路径,以使其能够完成。在 Set
中,它还会调用缓存条目上的扩展方法,因此也必须满足这一点。这会很快变得复杂,所以我建议使用具体的实现
//...
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
//...
public Test GetSystemUnderTest() {
var services = new ServiceCollection();
services.AddMemoryCache();
var serviceProvider = services.BuildServiceProvider();
var memoryCache = serviceProvider.GetService<IMemoryCache>();
return new Test(memoryCache);
}
[Fact]
public void TestCache() {
//Arrange
var sut = GetSystemUnderTest();
//Act
sut.SetCache("key", "value");
//Assert
//...
}
现在您可以访问功能齐全的内存缓存。
关于c# - 在单元测试中模拟 IMemoryCache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38318247/
我有一个 CacheService使用 GetOrCreateAsync基于键创建缓存。我正在缓存一个照片实体,它有一个 byte[]属性(property)。 这可以很好地缓存并按预期检索。但是,如
我正在尝试将我的 .Net 框架应用程序迁移到 .Net Core,在此过程中,我想将我的内存缓存从 System.Runtime.Caching/MemoryCache至Microsoft.Exte
我创建了一个全新的 ASP.NET Core Web API 项目。这是 Startup.cs 中的 ConfigureServices: public void ConfigureServi
我对 ASP.Net Core、C#、OOP、Javascript ……基本上是我目前使用的所有内容的新手。过去几个月我一直在阅读和学习以开始一个新的开发项目。总而言之,我正在稳步前进,但我遇到了 I
我正在使用 asp net core 1.0 和 xunit。 我正在尝试为一些使用 IMemoryCache 的代码编写单元测试。但是,每当我尝试在 IMemoryCache 中设置一个值时,我都会
我正在尝试模拟 IMemoryCache最小起订量。我收到此错误: An exception of type 'System.NotSupportedException' occurred in Mo
我目前正在执行以下操作以将列表存储在 IMemoryCache 中: _memoryCache.Set("PriceList", horsepricelist); 并访问它/返回数据: public
我正在创建一个 .net core 3.1 Worker Service 应用程序。我有一个使用 IMemoryCache 接口(interface)进行依赖注入(inject)的类。我有以下类的构造
我正在使用 IMemoryCache缓存从身份服务器检索到的 token 。 过去我用过 GetOrCreateAsync Microsoft.Extensions.Caching.Abstracti
我有一个从 Visual Studio 2015 Community Edition 创建的 ASP.NET Web 应用程序。 .NET Framework 是 4.6.1。 我不知道我的 Web
我一直在使用类似于示例 1 的操作来为我的 .NET Core API 异步缓存 json 结果。 MemoryCache 是 IMemoryCache 的实例。 示例 1(按预期工作): [Http
我正在尝试使用 Microsoft.Extensions.Caching.Memory.IMemoryCache 接口(interface)/类。 我需要向缓存中添加一个新项目,并确保我不会覆盖任何其
我有一个带有 API 的 ASP.NET Core MVC 项目。 然后我在名为 Infrastructure. 的同一个解决方案中有一个类库。 我的 API 在类库基础结构中调用存储库方法,在类 U
我们正在使用 .NET Core 3.1 和 Microsoft.Extensions.Caching.Memory.IMemoryCache (v3.1.24) 及其实现 Microsoft.Ext
.Net Core 为两个接口(interface)( MemoryCache 和 DistributedMemoryCache )提供内存实现,但假设我们有一个可用的 IDistributedCac
我知道 IMemoryCache.Set 是一个扩展方法,所以它不能被模拟。人们已经为这种情况提供了解决方法,例如 NKosi here 的解决方法。 .我想知道如何为我的 MemoryCache 返
我正在寻找一种方法来包装为某些数据调用远程 WCF 服务的服务。例如,这样的服务可能看起来像这样 public interface IAsyncSvc { Task GetData(int k
我正在编写一个简单的测试用例来测试我的 Controller 在调用我的服务之前调用缓存。我正在使用 xUnit 和 Moq 来完成这项任务。 我遇到了一个问题,因为 GetOrCreateAsync
我向 IMemoryCache 添加一个值 _cache.Set("Key", "Value", DateTimeOffset.UtcNow); 我想通过其他方法获取这个key的绝对过期值。 或者 我
我正在使用 IMemoryCache 将数据存储在缓存中...但它会不断向 SQL Server 发送 sql 调用,即使在缓存存在的情况下执行不会运行该代码。 我的查找服务 public class
我是一名优秀的程序员,十分优秀!