在 ASP.NET MVC 中对本地和全局资源进行单元测试
我有一个包装 GetGlobalResourceObject 和 GetLocalResourceObjet 的类,以便可以在 MVC 中轻松使用它们。 然后,模型验证类从资源文件动态加载错误消息。 问题是单元测试。 代码使用“〜/”,虽然在解决方案运行时一切正常,但我无法看到如何进行单元测试,因为我总是收到以下错误“System.Web.HttpException:应用程序相对虚拟路径'〜/ ' 不能设为绝对,因为应用程序的路径未知。”
引发异常的代码如下,用于计算表达式并返回全局资源对象。
Private Function GetExpressionFields(ByVal expression As String) As ResourceExpressionFields
Return GetExpressionFields(expression, "~/")
End Function
Private Function GetExpressionFields(ByVal expression As String, ByVal path As String) As ResourceExpressionFields
Dim context As New ExpressionBuilderContext(path)
Dim resource_builder As New ResourceExpressionBuilder()
Dim fields As ResourceExpressionFields
fields = DirectCast(resource_builder.ParseExpression(expression, GetType(String), context), ResourceExpressionFields)
Return fields
End Function
关于如何测试此代码和其他使用资源文件的代码有什么想法吗?
I have a class that wraps the GetGlobalResourceObject and GetLocalResourceObjet so they can be used easily in MVC. The model validation classes then load the error messages dynamically from resource files. The problem is unit testing. The code uses "~/", and while everything functions correctly when the solution is run, I cannot see how to make the unit tests because I always receive the following error "System.Web.HttpException: The application relative virtual path '~/' cannot be made absolute, because the path to the application is not known."
The code that throws the exception is the following, used to evaluate an expression and return a global resource object.
Private Function GetExpressionFields(ByVal expression As String) As ResourceExpressionFields
Return GetExpressionFields(expression, "~/")
End Function
Private Function GetExpressionFields(ByVal expression As String, ByVal path As String) As ResourceExpressionFields
Dim context As New ExpressionBuilderContext(path)
Dim resource_builder As New ResourceExpressionBuilder()
Dim fields As ResourceExpressionFields
fields = DirectCast(resource_builder.ParseExpression(expression, GetType(String), context), ResourceExpressionFields)
Return fields
End Function
Any ideas on how to test this and other code that uses resource files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我采取了稍微不同的方法。 我使用 App_* 目录之外的资源文件,在这种情况下,IDE 会向文件添加一个自定义工具,为解决方案中任何位置(包括视图)工作的资源生成强类型包装器,并在单元测试期间表现出来: http://odetocode.com/Blogs/scott/archive/ 2009/07/15/13211.aspx
这不是您问题的准确答案,但我相信让 App_* 资源在所有条件下都能正确运行是一项相当大的工作。
I took a slightly different approach. I use resource files outside of the App_* directories, in which case the IDE will add a custom tool to the file to generate a strongly typed wrapper for the resources that works anywhere in the solution, including in views, and behaves during unit tests: http://odetocode.com/Blogs/scott/archive/2009/07/15/13211.aspx
That's not an exact answer to your question, but I believe getting the App_* resources to behave correctly under all conditions was quite a bit of work.
您可以在 ResourceExtensions 类中创建一个属性,例如 bool IsInTestScope,然后在测试类中将其设置为 true,并在返回本地化文本的方法内部执行如下操作:
public static string Resource(this Controller controller, string expression, params object[] args)
{
if (!IsInTestScope)
{
ResourceExpressionFields fields = GetResourceFields(expression, "~/"); }
返回 GetGlobalResource(字段, 参数);
}
返回字符串.Empty;
}
You could create a property in your ResourceExtensions class, for example bool IsInTestScope, and then in your test class set it on true, and inside method which returns localized text do something like this:
public static string Resource(this Controller controller, string expression, params object[] args)
{
if (!IsInTestScope)
{
ResourceExpressionFields fields = GetResourceFields(expression, "~/");
return GetGlobalResource(fields, args);
}
return string.Empty;
}
我找到了测试使用 App_* 目录中的资源的代码的方法。
我在博客中描述了我的解决方案 链接文本< /a>
I've found approach for testing code that uses resources inside App_* directories.
I described my solution in my blog link text