刚开始尝试使用NUnit做单元测试,就碰上了一个难题,我怎样才能在NUnit测试函数中使用Application中的数据呢?
我现在是初始化了一个Page对象,但是它的Application仍然无法使用。

解决方案 »

  1.   

    Application variables are only available at runtime, you have to use mock objects, see the following articles. I didn't test, so not sure if it worksMocking the HttpContext object
    http://www.biasecurities.com/blogs/jim/archive/2005/08/11/2058.aspxHttpContext Provider - Free yourself of IIS!
    http://blogs.ittoolbox.com/visualbasic/dotnet/archives/005329.asphttp://codebetter.com/blogs/darrell.norton/archive/2004/05/23/14294.aspx
      

  2.   

    感谢思归兄,
    我现在可以在NUnit测试函数中用Application了,但是在这个函数中我还要测试一个页面(假定为index.aspx,index类继承System.Web.UI.Page )中的方法(假定为testApplication() ),在这个方法里也使用了Application,这时候就没法用了,在论坛上翻了一天,也没能解决这个问题。只能请您再次出马了。谢谢
      

  3.   

    you should start from testing the Page's ProcessRequest(HttpContext context) method, since that is how the code runs in ASP.NETif you want to test individual methods like that, you should structure your code like thisvoid MyMethod(HttpContext context)orvoid MyMethod(HttpApplicationState Application)
    ..
    then you can pass in the mock object, see, by thinking about unit testing, you actually change the way you design things
      

  4.   

    you should start from testing the Page's ProcessRequest(HttpContext context) method, since that is how the code runs in ASP.NET
    思归兄,这个该怎么弄阿,我英文不好,刚钢材试了半天还是不行,能否给点代码示例?
      

  5.   

    我的意思是,你需要提供类似HttpContext那样的mock object,但如果需要测试Page内的方法的话,你应该从设计上着手,也许需要重新设计你的对象里的方法,使得他们testable,而且考虑一下,Page类内的东西应该主要跟Controller以及View有关,跟Model有关的东西属于其他类,这样你就可以单独测试了,如果你需要测试View的话,建议参考ASP.NET unit testing
    http://nunitasp.sourceforge.net/