Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4110036
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:52:54+00:00 2026-05-20T21:52:54+00:00

I am trying to write a unit test for an Action who’s model uses

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:52:55+00:00Added an answer on May 20, 2026 at 9:52 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to write Unit test for Silverlight 4.0 using Moq 4.0.10531.7 public delegate void
I'm trying to write a unit test with phpunit for a model that uses
I'm trying to write a unit test that will loop through all action methods
I'm trying to write a unit test for a controller using Zend and PHPUnit
I'm trying to write a unit test for a sample project that uses the
I am using Moq for unit testing, and I am trying to write my
I am trying to write a unit test for a GUI application using the
I am trying to write a unit test for a multipart PUT using Symfony2
I am trying to write a unit test for an action method which calls
I am trying to write a unit test using .NET 4 to ensure that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.