Unit testing and resource files

Last post 10-03-2008 1:14 PM by Thejb. 4 replies.

Sort Posts:

  • Unit testing and resource files

    07-15-2006, 10:00 AM
    • Member
      45 point Member
    • ZoEpErCaViA
    • Member since 11-01-2005, 3:34 PM
    • Posts 9
    Short problem description: I'm using the App_GlobalResources folder and HttpContext.GetGlobalResourceObject(..) function to access my resources. My unit tests are in a separate project and I use NUnit for testing. The problem is that HttpContext.GetGlobalResourceObject(..) returns null in an offline (i.e. test setting).

    Details

    I guess the problem is that there's no HttpContext in the test setting. So I need to figure out how to access the resource file from my test classes. A complicating factor is that I'm not directly calling GetGlobalResourceObject from my web application but a helper function (which amongst others logs missing resources) on a class located in a different assembly.
    I guess I need to access the resource file using ResourceManager manually but I can't find out what parameters to pass to the constructor. My resources file is located in the App_GlobalResources file. Using Reflector I found out that that the actual resource location is in the assembly "App_GlobalResources", because this line is used by the auto-generated designer class:

    ResourceManager manager1 = new ResourceManager("Resources.MyProject", Assembly.Load("App_GlobalResources"));

    Unfortunately I cannot use the same line when I'm unit testing because it cannot find the assembly "App_GlobalResources".

    Has anyone any idea how I can access the resources in a web application in an offline (i.e. unit testing) setting?
    BTW I'm using Web Application Projects.
  • Re: Unit testing and resource files

    11-14-2006, 7:25 PM
    • Member
      392 point Member
    • donaldn
    • Member since 11-23-2002, 10:38 AM
    • New York, NY
    • Posts 82
    Did you ever get an answer to this post? I have the same problem. If you know the solution please reply/post it. Thanks!
  • Re: Unit testing and resource files

    10-01-2008, 8:52 PM
    • Member
      4 point Member
    • Thejb
    • Member since 10-01-2008, 8:46 PM
    • Posts 3
    I have also exactly the same problem and it's not easy to find any information on it.
    The website (MVC) works fine with the localization
    but now that i have implemented the localisation, my unit test break.
    Thank you.. I will continue to search ... but if you know now...please reply :) JB
  • Re: Unit testing and resource files

    10-03-2008, 9:09 AM
    • Member
      392 point Member
    • donaldn
    • Member since 11-23-2002, 10:38 AM
    • New York, NY
    • Posts 82

    sounds like you need to simulate httpcontext for unit tests - see this link or google for more http://haacked.com/archive/2005/06/11/simulating_httpcontext.aspx

  • Re: Unit testing and resource files

    10-03-2008, 1:14 PM
    • Member
      4 point Member
    • Thejb
    • Member since 10-01-2008, 8:46 PM
    • Posts 3
    Thanks, great thanks to you donaldn and Phil Haack for the mock of the httpContext.
Page 1 of 1 (5 items)