i'm trying unittest controller exception catch exceptionfilterattribute , launched httpresponseexception.
controller
[exceptionfilters] //exceptionfilterattribute public class elevecontroller : apicontroller { private igpirepository _gpirepository; public elevecontroller(igpirepository gpirepository) { _gpirepository = gpirepository; } [httpget] [route("{fiche:int}/grouperepere")] public grouperepere grouperepere(int fiche) //this im trying test { gpiservice service = new gpiservice(_gpirepository); return service.get(fiche); //throw argumentnullexception when fiche == 0 } } exceptionfilter
public class exceptionfilters : exceptionfilterattribute { public override void onexception(httpactionexecutedcontext context) { if (context.exception notimplementedexception) { context.response = new httpresponsemessage(httpstatuscode.notimplemented); } else if (context.exception argumentnullexception) { context.response = new httpresponsemessage(httpstatuscode.badrequest) { content = new stringcontent(string.format("argument \"{0}\" null or invalid", ((argumentnullexception)context.exception).paramname)), reasonphrase = "argument null or invalid" }; } } and test:
private igpirepository _gpirepository; private mock<icallapi> _callapi; private elevecontroller _controller; [testinitialize] public void initialize() { _callapi = new mock<icallapi>(); _gpirepository = new gpirepository(_callapi.object); _controller = new elevecontroller(_gpirepository); } [testmethod] public void elevecontroller__grouperepere_withbadfiche_400badrequest() { string nogroupe = "111"; int fiche = 0; try { grouperepere gp = _controller.grouperepere(fiche); assert.fail(); } catch (exception e) { assert.istrue(e httpresponseexception); // not working --> argumentnullexception } } the problem e still argumentnullexception. when go on debug, doesn't reach exceptionfilter class
am missing something? thanks.
your test directly against controller. exceptionfilterattribute depends on server.(remember: attributes metadata)
the way test behavior use iis server or selfhost server, raise server in test class , send request:
[testinitialize] public void initialize() { _callapi = new mock<icallapi>(); _gpirepository = new gpirepository(_callapi.object); //initialize server //set _gpirepository dependency , etc.. } [testmethod] public void elevecontroller__grouperepere_withbadfiche_400badrequest() { //configure request var result = client.executeasget<grouperepere>(<your request>); assert.areequal(httpstatuscode.badrequest,result.statuscode); } in opinion shouldn't error code unless controller apart of public api.(the reason simple kind of tests simple break, thay slow , thay use expensive resources) if controller part public api should test through acceptance tests, verify nothing override expected behavior.
if still want test behavior i'd offer alternative way test it:
- create ut against
exceptionfilters. - create ut verifies method has
exceptionfiltersattribute
for example:
[testmethod] public void grouperepere_hasexceptionfiltersattribute() { var attribute = typeof (unittest2).getmethod("grouperepere").getcustomattributes(true); foreach (var att in attribute) { if(att.gettype() typeof(exceptionfilters)) { return; } } assert.fail(); } pros: it' fast, not easy break, doesn't use expensive reasorces.
cons: in production setting override expected behavior.
Comments
Post a Comment