user1639762 user1639762 - 23 days ago 8
ASP.NET (C#) Question

Link resx files in Unit Test

I'm working on a unit test project in Visual Studio 2012 for an ASP.NET MVC 4 web application.
The solution has 3 projects:


  • AdminUI

  • UnitTests

  • WebUI



The WebUI has some resx files for locatization inside the App_GlobalResources folder and it uses:

HttpContext.GetGlobalResourceObject("Resources", key).ToString()


to get the strings.

Now I need to unit test an action method that uses localization.
I would like to get the strings while unit testing the method.

Here's what I did

I managed to mock the GetGlobalResourceObject method and I can send any string I need

I added the Resources.resx file to the UnitTests project as a link

I edited the properties of the link:


  • Build action: Embedded Resource

  • Copy to output directory: do not copy

  • Custom tool: GlobalResourceProxyGenerator



I actually can retrieve the strings using:

Resources.stringName


but, if I add a new string in the original resx file (the one in the WebUI project), everything breaks up and I can't build the solution.

Isn't the link supposed to reflect the original file?

Answer Source

What you are describing is not really a unit test. A unit test should not relay on external resources.

When you say "I managed to mock GetGlobalResourceObject method", I assume that you are working with HttpContextBase.GetGlobalResourceObject (accessible in your controller).

So, for unit test purposes, you don't need any resx files. Just setup the proper calls on that mocked method, so it returns whatever is needed.