我正在使用asp net core 1.0和xunit.
我正在尝试为一些使用的代码编写单元测试IMemoryCache
.但是每当我尝试在I中设置一个值时,IMemoryCache
我都会得到一个Null参考错误.
我的单元测试代码是这样的:
将IMemoryCache
注入到我想要测试的类中.但是,当我尝试在测试中设置缓存中的值时,我得到一个空引用.
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
}
Run Code Online (Sandbox Code Playgroud)
这是班级测试......
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)});
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是......我需要以IMemoryCache
某种方式设置吗?为DefaultValue设置一个值?当IMemoryCache
被嘲笑什么是默认值?
我试图在ASP.NET Core项目中使用资源(.resx)文件.我使用namedResource在rc1中工作,但似乎无法在RTM中使用它.这是我的project.json的样子:
"buildOptions": {
"embed": {
"include": [ "Resources/resource.resx" ]
}
}
Run Code Online (Sandbox Code Playgroud)
我正在这样访问它:
Resources.ResourceManager.GetString("Field1");
Run Code Online (Sandbox Code Playgroud)
似乎资源没有嵌入,因为我调试时什么也没有.
任何帮助将非常感激.
我目前正在将项目从.NET Core RC1升级到新的RTM 1.0版本.在RC1中,有一个在版本1.0中IApplicationEnvironment
被替换IHostingEnvironment
在RC1我可以做到这一点
public class MyClass
{
protected static IApplicationEnvironment ApplicationEnvironment { get;private set; }
public MyClass()
{
ApplicationEnvironment = PlatformServices.Default.Application;
}
}
Run Code Online (Sandbox Code Playgroud)
有谁知道如何在v1.0中实现这一目标?
public class MyClass
{
protected static IHostingEnvironment HostingEnvironment { get;private set; }
public MyClass()
{
HostingEnvironment = ???????????;
}
}
Run Code Online (Sandbox Code Playgroud)