Is there a way to test code in catch statement, the trick is the catch ends with a throw
and this is where things go side ways. So the question is,is there a way to avoid that throw when running my Test method or maybe accomodate it on my test?
The method below just simply updates values and on catch it has a throw
public void UpdateCase(LinqToSQLModelName record, IProgress<string> statusReporter)
{
try
{
statusReporter.Report(record.Reference);
_service.PushToService(record);
record.ProcessedDate = DateTime.Now;
record.StatusId = (int)Status.Processed;
}
catch (Exception ex)
{
record.StatusId = (int)Status.Fail;
record.ErrorExceptions = ex.StackTrace;
record.ProcessedDate = DateTime.Now;
Result = false;
throw ex;//How do dodge this when running my test/ Accomodate it on my Test
}
finally
{
_db.SubmitChanges();
}
}
I need need to test the catch part now, test below
[TestMethod()]
public void UpdateCaseErrorTest()
{
var repository = new Mock<IDataContext>();
var errorLoggerRepository = new Mock<IExceptionLogger>();
var serviceRepository = new Mock<IServiceInterface>();
repository.Setup(m => m.Get()).Returns(new
ManualWithDrawDataContext());
repository.Setup(m => m.GetCouncilRefundRecord())
.Returns(new LinqToSQLModelName
{
refrence= "10",
Reason = "Reason",
DateCaptured = DateTime.Now,
});
var sut = new DataContext(repository.Object.Get(), serviceRepository.Object, errorLoggerRepository.Object);
sut.UpdateCase(repository.Object.GetCouncilRefundRecord(), null);//This null allows me to go to the catch
Assert.IsFalse(sut.Result);
}