2011-03-22 6 views
1

누가 데이터 유효성 검사를 사용하여 해당 멤버의 유효성을 검사하는 액션의 단위 테스트를 작성하려고합니다. 내 조롱 프레임 워크로 Moq을 사용하고 있습니다.ASP.NET + Moq + 데이터 주석 + 리소스 문자열

속성에 대한 [필수] 오류 메시지를 '하드 코드'하면 모든 것이 잘 작동합니다. 나는 내 테스트를 잘 실행할 수 있으며, 테스트 결과가 기대된다. 그러나 리소스 파일에서 오는 오류 메시지가 필요합니다. 그래서 그 대신 일을 :

[Required(ErrorMessage = "First Name is required")] 
    public string FirstName 
    { 
     get; 
     set; 
    } 

을 내가 대신이 작업을 수행 할 필요가 : 나는 자원을 기반으로 문자열을 사용하는 경우, 그것은 잘 작동

[Required(ErrorMessageResourceName = "Account_FirstNameRequired", ErrorMessageResourceType = typeof(Resources.ModelValidationErrors))] 
    public string FirstName 
    { 
     get; 
     set; 
    } 

.. 그러나, 나는 내 테스트를 실행하려고하면, I 다음 오류가 발생합니다.

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].

틀림없이 Moq를 처음 사용했습니다. 필자는 검색을했으며 리소스 주석으로 데이터 주석을 제외하고는 데이터 주석이있는 Moq의 예를 찾지 못했습니다. 누군가 내가 잘못하고있는 것을 말해 줄 수 있습니까?

답변

2

내부적으로 App_GlobalResources은 단위 테스트에서 사용할 수없는 HttpContext을 사용합니다. 이 문제에 관해 이야기하는 blog post은 다음과 같습니다. 요약하면

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

제안 된 솔루션은 리소스 파일을 특수 리소스 디렉토리 외부로 이동하는 것입니다.

+0

감사합니다. 대린, 블로그 게시물이 내 질문에 대한 답변의 자리였습니다. 매우 감사! – Sharbel

관련 문제