I have a question that may seem stupid and simple, but I hardly have any idea how to proceed with it.
My question is:
How can I modify the exception message and customize it such that I still have my unit testing passing?
Actually I want to customize the exception message to "Student "Johny" had related files!" and as modified the API exception message, the unit testing failing.
Johny is a variable that may change...
Any help how I can achieve the above. Thanks
In my test class I am having
[ExpectedException(ExceptionType = typeof(Exception), ExpectedMessage = "The DELETE statement conflicted with the REFERENCE constraint \"FK_Issue_Priority\"")]
Actually I am using NHibernate and in my API I am handling exception as follows:
catch (NHibernate.ADOException exception)
{
if (exception.InnerException.GetType().Equals(typeof(System.Data.SqlClient.SqlException)))
{
if (exception.InnerException.Message.Contains("FK_Issue_Priority"))
{
throw new Exception("The DELETE statement conflicted with the REFERENCE constraint \"FK_Issue_Priority\"");
}
else
{
throw new Exception("A database error occurred while trying to add the customer to project relation please the see inner exception for details", exception.InnerException);
}
}
else
{
throw exception;
}
}
I don't test the exact content of exception messages in my unit tests for exactly this reason -- they tend to be variable.
Instead you have two options:
Derive a new
Exception
based class expressly for throwing in this method (for example a 'RelatedFilesExistedException' class). Unit testing can simply check that the correct exception type is being returned without having to worry about exactly matching the message text.Only partially match the exception message (for which you will have to write your own testing code and not reply on the
ExpectedException
attribute).