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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top