Testing for ArgumentNullException
If you remove laid-back
catch (Exception e) { throw new Exception(e.Message); }
from your test code (the current catch loses the error context and breaks the stack trace, see below), your test can be as simple as transferring a call to Assert.Throws<ArgumentNullException>() :
[Test] public void PassingANullEntityToAddMustThrowArgumentNullException() { var documentStatusService = new DocumentStatusService(...); Assert.Throws<ArgumentNullException>(() => documentStatusService.Add(null)); }
Re: Your exception handler
In your service code, never catch the exception and do not collapse it the way you did, as this will lose the stack trace (for example, _repository.Add(entity); may also be _repository.Add(entity); .). You also do not add any value by throwing e.Message , as it is already in the original exception (with additional information such as stack trace and internal exception)
Bad:
catch (Exception e) { throw new Exception(e.Message); }
Better: if you catch and pritronet with some value, wrap the original as an internal exception:
catch (SqlException ex) { throw new Exception("Some value add here", ex); }
or if you just intercept and allow distribution:
catch (SqlException) { // Do some logging throw; }
It would be best to allow the exception to be thrown if you are not adding a value or handling it.
source share