ASP.NET +起订量 +数据注释 +资源字符串
我正在尝试为模型使用数据注释来验证成员的操作编写单元测试。我使用 Moq 作为我的模拟框架。
当我对属性的 [Required] 错误消息进行“硬编码”时,一切都运行良好。我能够很好地运行我的测试,并且测试结果是预期的。但是,我需要来自资源文件的错误消息。因此,我不需要这样做:
[Required(ErrorMessage = "First Name is required")]
public string FirstName
{
get;
set;
}
我需要这样做:
[Required(ErrorMessageResourceName = "Account_FirstNameRequired", ErrorMessageResourceType = typeof(Resources.ModelValidationErrors))]
public string FirstName
{
get;
set;
}
当我使用基于资源的字符串时,它工作正常..但是,当我尝试运行测试时,我收到以下错误:
测试方法 MyProject.Tests.Controllers.AdminAccountsTest.AdminAccounts_Create_Calls_Save 引发异常: System.Reflection.TargetInitationException:调用目标已引发异常。 ---> System.IO.FileNotFoundException:无法加载文件或程序集“App_GlobalResources”或其依赖项之一。系统找不到指定的文件。警告:程序集绑定日志记录已关闭。 要启用程序集绑定失败日志记录,请将注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) 设置为 1。 注意:程序集绑定失败日志记录会带来一些性能损失。 要关闭此功能,请删除注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog]。
不可否认,我是使用起订量的新手。我进行了搜索,但没有找到任何带有数据注释的起订量示例,更不用说带有资源字符串的数据注释了。有人可以告诉我我做错了什么吗?
I am trying to write a unit test for an Action who's model uses Data Annotations to validate the members. I am using Moq as my mocking framework.
When I 'hard-code' the [Required] error message for the property, everything works great. I am able to run my test just fine, and the test results are expected. However, I need to have the error messages coming from a resource file. So instead of doing:
[Required(ErrorMessage = "First Name is required")]
public string FirstName
{
get;
set;
}
I need to do this instead:
[Required(ErrorMessageResourceName = "Account_FirstNameRequired", ErrorMessageResourceType = typeof(Resources.ModelValidationErrors))]
public string FirstName
{
get;
set;
}
When I use the Resource based string, it works fine.. however, when I try to run my test, I get the following error:
Test method MyProject.Tests.Controllers.AdminAccountsTest.AdminAccounts_Create_Calls_Save threw exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileNotFoundException: Could not load file or assembly 'App_GlobalResources' or one of its dependencies. The system cannot find the file specified.WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
Admittedly, I am new to using Moq. I have searched and I didn't have much luck in finding any examples of Moq with Data Annotations, let alone Data Annotations with Resource strings. Can someone please tell me what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在内部,
App_GlobalResources
依赖于在单元测试中不可用的HttpContext
。这是一个 博客帖子讨论了这个问题。简而言之:建议的解决方案是将资源文件移到特殊资源目录之外。
Internally
App_GlobalResources
relies on anHttpContext
which is not available in unit tests. Here's a blog post that talks about this issue. In short:The proposed solution is to move the resource files outside of special resource directories.