asp.net - How can I write a unit test against a WebPartBase class and mock out the controls? -
i have bunch of webpartbase classes i'm trying invoke method on. of methods phone call controls on ui. want stub out controls somehow exception doesn't thrown when attempting set values on them. know of way this? fine if initialized i'm not sure how initialize them without running web server.
my test code looks this. intializeonadd() method on kpiwebpartbase , kpiwebpartbase inherits webpartbase.
[testmethod] public void invokeallwidgets() { var user = new user("adminqaphi"); user.customerid = testconfig.clientid; var classes = typeof(kpiwebpartbase) .assembly .gettypes() .where(t => t.issubclassof(typeof(kpiwebpartbase)) && !t.isabstract ); foreach (var c in classes) { console.writeline(c.name); var instance = (kpiwebpartbase)activator.createinstance(c); foreach (var billingmetric in enum.getvalues(typeof(billingmetric))) { instance.currentuser = user; instance.billingmetric = (billingmetric)billingmetric; if (instance startendkpiwebpartbase) { var startendkpi = (startendkpiwebpartbase)instance; startendkpi.startdate = new datetime(2007, 1, 1); startendkpi.enddate = new datetime(2008, 1, 1); } instance.initializeonadd(); } } }
i create interfaces them (if possible) , utilize moq create mocks or stubs external dependencies.
what behavior invokeallwidgets() test?
response comment: think should mock database well, test "internal" logic of code(unit). otherwise en testing database, , guess not want to. , if phone call database, happens if info in changes? fail tests? if think doing integration tests , not unit tests. in unit test seek mock external dependencies , test own logic (what can go wrong?). if code don't allow test like, alter it!
asp.net unit-testing tdd web-parts
No comments:
Post a Comment