开发者

c# (asp.net mvc): Testing a controller that uses a variable from global.asax?

开发者 https://www.devze.com 2023-03-27 05:35 出处:网络
I have a controller action that uses a static variable on Global.asax which is filled in by Application_Start....

I have a controller action that uses a static variable on Global.asax which is filled in by Application_Start....

here is the simple sample code

    string url = VirtualPathUtility.Combine(MvcApplication.BaseU, "/geturl");

But of course in a Test when i am testing my controller the MvcApplication.BaseU is NULL

as its not called and filled in by Application_Start from the global.asax

What is the best option for doing this c开发者_C百科orrectly?

Any ideas really helpful


I have a controller action that uses a static variable on Global.asax which is filled in by Application_Start

That's bad. You could refactor your code so that your controller takes this value as constructor parameter (constructor injection) and then configure your DI framework to pass the proper value. Now in your unit test it is a simple matter of passing a mocked value for the test.

0

精彩评论

暂无评论...
验证码 换一张
取 消