Wednesday, 17 February 2010

Resx Resource Problems when Unit Testing

If you use resx files for something like translations in your web apps, you'll notice problems when unit testing as your web app won't be running in its natural state and those resources won't be loaded as normal. Typically you'll get the error...

Could not load file or assembly App_GlobalResources

I'm yet to find a decent solution for this problem but here's how I currently get around it in MVC apps. Here I'm replacing my dependency on Strings.resx in App_GlobalResources.

First thing to do is to create a ResourceManager property and make use of this instance within the class you're testing. Now whenever you need to make use of the resource you have to replace the normal syntax of Strings.FieldYouWant with StringsResource.GetString("FieldYouWant") as shown in the method MethodBeingTested().

public class MyController
   public ResourceManager StringsResource
      private set;

   public MyController()
       : this(Strings.ResourceManager)

   public MyController(ResourceManager stringsResourceManager)
      StringsResource = stringsResourceManager;

   public object MethodBeingTested()
      return new ContentResult( StringsResource.GetString("FieldYouWant") );

In your test project you'll need to create a Strings.resx mirrored source file and make use of this when testing MyController class. Do this by instantiating with a resource from the calling test assembly...

var stringsResource
   = new ResourceManager("test.assembly.Strings", Assembly.GetExecutingAssembly());
var controller = new MyController(stringsResource);
...not pretty, but at least you can run your unit tests without an exception slapping you around the face. Any one got a better solution?


  1. Try using this attribute in the test method:

    [DeploymentItem(@"Project\bin\Debug\ca", "ca")]


  2. Is this an issue with all RESX files, or just those in the App_GlobalResources?

    I've been using the localisation pattern set out here: and not had any problems with testing.