unit testing - Trying to expose additional information when using xUnit Assert.Throws -
i'm setting first unit tests, , can't quite see how i'm trying achieve (with current test structure) can done, means i'm not sure whether approach tests incorrect, or it's limitation on xunit.
i'm testing mvc controllers, , want ensure provide argumentnullexception if constructed passing null across arguments (they resolved via castle in real world).
so, i've private field on test class:
private ienumerable<type> controllertypes = typeof(mybasecontroller).assembly.gettypes().where(t => iscontroller(t)); then, test method:
[fact] public void ensurecontrollersthrowifprovidedwithnull() { foreach (var controller in controllertypes) { var ctrs = getconstructorsfortype(controller); if (null == ctrs || !ctrs.any()) { //if controller has no constructors, that's fine, skip on continue; } var ctr = ctrs.elementat(0); var ctrparamsasnull = ctr.getparameters().select(p => (object)null); assert.throws<argumentnullexception>(() => { ctr.invoke(ctrparamsasnull.toarray()); }); } } so working fine, run test runner, , 1 of controllers doesn't throw argumentnullexception when passed null, great, test fails, don't know controller was, given output.
i know how can debug through test see fails, , can manually go through controllers check is, useful know controller failed.
or using unit test wrong here?
(side note, there's test ensures there's 1 public constructor each controller, can sure i'm targeting correct constructor when fires, long first test passed).
thanks
note: there's flaw in logic test, means doesn't cover expecting too, long throws argumentnullexception @ least 1 of arguments, pass test, isn't right. arguments interfaces can't instantiate new instance of them. looking copy code test, wouldn't so. not looking solution issue here.
assert.throws helper method executes delegate inside try catch block. don't have use , can replace own implementation. like:
[fact] public void ensurecontrollersthrowifprovidedwithnull() { foreach (var controller in controllertypes) { var ctrs = getconstructorsfortype(controller); if (null == ctrs || !ctrs.any()) { //if controller has no constructors, that's fine, skip on continue; } var ctr = ctrs.elementat(0); var ctrparamsasnull = ctr.getparameters().select(p => (object)null); book ok = false; try { ctr.invoke(ctrparamsasnull.toarray()); } catch(argumentnullexception) { //you exception expected continue ok = true; } if(!ok) { // didn't exception throw own exception message contains controller type name throw new exception(string.format("ctor on type {0} did not throw argumentnullexception",controller.name); } } } this idea work on. can refactor inside own static assertion method...
Comments
Post a Comment