Unit Testing an ASP.NET MVC Action Which Accesses the Bundles -


here's code in action method:

var bundleurl = scripts.url("~/scripts/mybundle"); 

when try , unit test action method, following error/stack trace:

system.argumentnullexception value cannot null. parameter name: httpcontext    @ system.web.httpcontextwrapper..ctor(httpcontext httpcontext)    @ system.web.optimization.scripts.url(string virtualpath) 

any ideas?

i've mocked httpcontext appropriately, request filled, can access routes, etc.

do need setup bundles? (like on global.asax)

edit:

after using ilspy @ source code system.web.optimization, appears scripts class creates httpcontextwrapper static httpcontext.current instance (which null).

because scripts class uses static system.web.httpcontext.current instance, near impossible mock.

so ended creating wrapper interface scripts, mocked out in unit tests.

e.g

var bundleurl = scripts ?? new scriptswrapper()..url("~/scripts/mybundle"); 

unit test:

controller.scripts = new mock<iscripts>().object; 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -