ASP.NET +起订量 +数据注释 +资源字符串

发布于 2024-10-25 07:24:35 字数 1139 浏览 5 评论 0原文

我正在尝试为模型使用数据注释来验证成员的操作编写单元测试。我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳若烟 2024-11-01 07:24:35

在内部,App_GlobalResources 依赖于在单元测试中不可用的 HttpContext。这是一个 博客帖子讨论了这个问题。简而言之:

避免 App_GlobalResources 和
App_LocalResources(它有自己的
MVC 中的一组问题。

建议的解决方案是将资源文件移到特殊资源目录之外。

Internally App_GlobalResources relies on an HttpContext which is not available in unit tests. Here's a blog post that talks about this issue. In short:

avoid App_GlobalResources and
App_LocalResources (which has its own
set of problems) in MVC.

The proposed solution is to move the resource files outside of special resource directories.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文