asp.net mvc - Unit testing all controllers from a single test -
i created action filter want apply of controllers (including new ones introduced later on).
i figure useful unit test 1 cycles through each controller , verifies if criteria met, action filter impact result.
is wise create unit test hits multiple controllers? can share code similar test has proven useful?
edit: realized testing action filter might problematic. still, if have thoughts share on mass testing of controllers...
it not recommended test more 1 thing @ time in tests.
you should avoid logic in tests (switch, if, else, foreach, for, while) test less readable , possibly introduces hidden bugs.
many simple, readable, , therefore maintainable tests testing 1 thing each far preferable 1 test lot of complexity.
response edit
testing filters can achieved separating filter attribute. here example: loadmembershiptypelistfilter class has 'seams' needed use test fakes. logic in filter is tested.
public class loadmembershiptypelistfilter : iactionfilter { private imembershiptypeprovider _provider; private imembershiptypeadminmapper _mapper; public loadmembershiptypelistfilter(imembershiptypeprovider provider, imembershiptypeadminmapper mapper) { _provider = provider; _mapper = mapper; } #region iactionfilter members public void onactionexecuted(actionexecutedcontext filtercontext) { } public void onactionexecuting(actionexecutingcontext filtercontext) { //implementation... } #endregion }
and attribute here uses filter, example resolves dependencies filter requires call service locator:
public class loadmembershiptypelistattribute : actionfilterattribute { public override void onactionexecuting(actionexecutingcontext filtercontext) { var filter = new loadmembershiptypelistfilter(iocresolve.resolve<imembershiptypeprovider>(), iocresolve.resolve<imembershiptypeadminmapper>()); filter.onactionexecuting(filtercontext); } }
and controller uses attribute:
[loadmembershiptypelist] public actionresult create() { return view(); }
Comments
Post a Comment