gpt4 book ai didi

asp.net-mvc - "App_GlobalResources"未在单元测试用例中加载

转载 作者:行者123 更新时间:2023-12-03 13:57:53 25 4
gpt4 key购买 nike

我有一个测试 Controller 操作方法的单元测试方法。 action 方法使用资源文件来获取静态消息。

 message = Resources.MyResource.MemberNotVerified;

然而,在这一行抛出的异常是: -

"Could not load file or assembly 'App_GlobalResources' or one of its dependencies. The system cannot find the file specified.":"App_GlobalResources" System.IO.IOException {System.IO.FileNotFoundException}



我尝试在我的测试项目中处理整个资源文件,但没有成功。
有什么想法的 friend 。

最佳答案

这是一个不需要更改任何内容的解决方案,因为它会生成一个名为 App_GlobalResources.dll 的程序集。嵌入所有资源,测试期望的方式。

只需从标有 AssemblyInitialize 的方法调用它属性,它只会在所有测试开始之前运行一次:

public static void GenerateResourceAssembly()
{
var testExecutionFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

var solutionRootPath = "PATH_TO_YOUR_SOLUTION_ROOT";

//Somewhere similar to C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin
var pathResgen = "PATH_TO_RESGEN.EXE";

//You may need to adjust to the path where your global resources are
var globalResourcesPath = Path.Combine(solutionRootPath, @"Web\App_GlobalResources");

var parameters = new CompilerParameters
{
GenerateExecutable = false,
OutputAssembly = "App_GlobalResources.dll"
};

foreach (var pathResx in Directory.EnumerateFiles(globalResourcesPath, "*.resx"))
{
var resxFileInfo = new FileInfo(pathResx);

var filename = resxFileInfo.Name.Replace(".resx", ".resources");

var pathResources = Path.Combine(testExecutionFolder, "Resources." + filename);

var startInfo = new ProcessStartInfo
{
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
FileName = Path.Combine(pathResgen, "resgen.exe"),
Arguments = string.Format("\"{0}\" \"{1}\"", pathResx, pathResources)
};

using (var resgen = Process.Start(startInfo))
{
resgen.WaitForExit();
}

parameters.EmbeddedResources.Add(pathResources);
}

CodeDomProvider.CreateProvider("CSharp").CompileAssemblyFromSource(parameters);
}

关于asp.net-mvc - "App_GlobalResources"未在单元测试用例中加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4153748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com