小编Bil*_*ers的帖子

在单元测试中模拟IMemoryCache

我正在使用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被嘲笑什么是默认值?

c# unit-testing moq mocking asp.net-core

17
推荐指数
3
解决办法
8715
查看次数

在.NET Core 1.0中使用resx资源

我试图在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)

似乎资源没有嵌入,因为我调试时什么也没有.

任何帮助将非常感激.

asp.net-core-mvc asp.net-core

11
推荐指数
1
解决办法
8226
查看次数

在单元测试中设置IHostingEnvironment

我目前正在将项目从.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)

c# unit-testing .net-core asp.net-core .net-core-rc1

10
推荐指数
3
解决办法
9024
查看次数