Last post Feb 25, 2016 06:40 PM by nicolas89
Feb 25, 2016 12:39 AM|nicolas89|LINK
At the moment I have a mock db in memory and doing sth like :
var result = _controller.GetMyStuff(1) as ObjectResult;
var oneItem = result.Value as OneThing;
how should I handle exceptions ? for example if my controller returns " return HttpBadRequest(); " how do i test this?
var invresult = _controller.GetMyStuff(100000) as ObjectResult;
Assert.Equal(200, invresult.StatusCode); => does not work, the object is null. Exception raised
note that I would not expect 200 here but soem other code.. 404 etc..
Also I got my REST api generated and was surprised to see the generted getXXX not using try catch around for example
for example a call for a MySuff object with a non existing id will return an exception. That should return HttpBadRequest or HttpNotFound afaik.
Thanks for you lights in advance
Feb 25, 2016 02:00 AM|bruce (sqlwork.com)|LINK
in mvc framework, a try/catch is used when calling the action, if it throws and error, it will return bad request.
to test bad id, the mock db should return not found, so the controller will throw an error. the unit test should assert that the error is thrown.
note: you are not testing http responses, you are testing that the controller actions return to the framework the expected values.
Feb 25, 2016 06:40 PM|nicolas89|LINK
Understood, we don t need to catch everything. I guess I am overthinking it or simply just surprised to see the generated REST controllers are not checking the object returned by a function such as Single().
note : the mock db will return NULL (same as real model when using single()).